Greg Kroah-Hartman | 619daee | 2018-01-22 16:18:13 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Tejun Heo | 6d66f5c | 2007-09-20 17:31:38 +0900 | [diff] [blame] | 3 | * fs/sysfs/file.c - sysfs regular (text) file implementation |
| 4 | * |
| 5 | * Copyright (c) 2001-3 Patrick Mochel |
| 6 | * Copyright (c) 2007 SUSE Linux Products GmbH |
| 7 | * Copyright (c) 2007 Tejun Heo <teheo@suse.de> |
| 8 | * |
Mauro Carvalho Chehab | 0c1bc6b | 2020-04-14 18:48:37 +0200 | [diff] [blame] | 9 | * Please see Documentation/filesystems/sysfs.rst for more information. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/kobject.h> |
Robert P. J. Day | c6f8773 | 2008-03-13 22:41:52 -0400 | [diff] [blame] | 14 | #include <linux/slab.h> |
Oliver Neukum | 94bebf4 | 2006-12-20 10:52:44 +0100 | [diff] [blame] | 15 | #include <linux/list.h> |
Dave Young | 52e8c20 | 2007-07-26 11:03:54 +0000 | [diff] [blame] | 16 | #include <linux/mutex.h> |
Tejun Heo | 13c589d | 2013-10-01 17:42:02 -0400 | [diff] [blame] | 17 | #include <linux/seq_file.h> |
Joe Perches | 2efc459 | 2020-09-16 13:40:38 -0700 | [diff] [blame] | 18 | #include <linux/mm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
| 20 | #include "sysfs.h" |
Tejun Heo | f6acf8b | 2013-11-28 14:54:21 -0500 | [diff] [blame] | 21 | |
| 22 | /* |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 23 | * Determine ktype->sysfs_ops for the given kernfs_node. This function |
Tejun Heo | 375b611 | 2013-10-01 17:41:57 -0400 | [diff] [blame] | 24 | * must be called while holding an active reference. |
| 25 | */ |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 26 | static const struct sysfs_ops *sysfs_file_ops(struct kernfs_node *kn) |
Tejun Heo | 375b611 | 2013-10-01 17:41:57 -0400 | [diff] [blame] | 27 | { |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 28 | struct kobject *kobj = kn->parent->priv; |
Tejun Heo | 375b611 | 2013-10-01 17:41:57 -0400 | [diff] [blame] | 29 | |
Tejun Heo | df23fc3 | 2013-12-11 14:11:56 -0500 | [diff] [blame] | 30 | if (kn->flags & KERNFS_LOCKDEP) |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 31 | lockdep_assert_held(kn); |
Tejun Heo | 375b611 | 2013-10-01 17:41:57 -0400 | [diff] [blame] | 32 | return kobj->ktype ? kobj->ktype->sysfs_ops : NULL; |
| 33 | } |
| 34 | |
Tejun Heo | 13c589d | 2013-10-01 17:42:02 -0400 | [diff] [blame] | 35 | /* |
| 36 | * Reads on sysfs are handled through seq_file, which takes care of hairy |
| 37 | * details like buffering and seeking. The following function pipes |
| 38 | * sysfs_ops->show() result through seq_file. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | */ |
Tejun Heo | c2b19da | 2013-11-28 14:54:16 -0500 | [diff] [blame] | 40 | static int sysfs_kf_seq_show(struct seq_file *sf, void *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | { |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 42 | struct kernfs_open_file *of = sf->private; |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 43 | struct kobject *kobj = of->kn->parent->priv; |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 44 | const struct sysfs_ops *ops = sysfs_file_ops(of->kn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | ssize_t count; |
Tejun Heo | c2b19da | 2013-11-28 14:54:16 -0500 | [diff] [blame] | 46 | char *buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
Christoph Hellwig | 820879e | 2021-09-13 07:41:14 +0200 | [diff] [blame] | 48 | if (WARN_ON_ONCE(!ops->show)) |
| 49 | return -EINVAL; |
| 50 | |
Tejun Heo | f5c16f2 | 2014-05-19 15:52:10 -0400 | [diff] [blame] | 51 | /* acquire buffer and ensure that it's >= PAGE_SIZE and clear */ |
Tejun Heo | 13c589d | 2013-10-01 17:42:02 -0400 | [diff] [blame] | 52 | count = seq_get_buf(sf, &buf); |
| 53 | if (count < PAGE_SIZE) { |
| 54 | seq_commit(sf, -1); |
| 55 | return 0; |
| 56 | } |
Tejun Heo | f5c16f2 | 2014-05-19 15:52:10 -0400 | [diff] [blame] | 57 | memset(buf, 0, PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
Christoph Hellwig | 820879e | 2021-09-13 07:41:14 +0200 | [diff] [blame] | 59 | count = ops->show(kobj, of->kn->priv, buf); |
| 60 | if (count < 0) |
| 61 | return count; |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 62 | |
Miao Xie | 8118a85 | 2007-11-21 14:55:19 -0800 | [diff] [blame] | 63 | /* |
| 64 | * The code works fine with PAGE_SIZE return but it's likely to |
| 65 | * indicate truncated result or overflow in normal use cases. |
| 66 | */ |
Andrew Morton | 815d2d5 | 2008-03-04 15:09:07 -0800 | [diff] [blame] | 67 | if (count >= (ssize_t)PAGE_SIZE) { |
Sergey Senozhatsky | 9e6d35f | 2017-12-11 21:50:22 +0900 | [diff] [blame] | 68 | printk("fill_read_buffer: %pS returned bad count\n", |
| 69 | ops->show); |
Andrew Morton | 815d2d5 | 2008-03-04 15:09:07 -0800 | [diff] [blame] | 70 | /* Try to struggle along */ |
| 71 | count = PAGE_SIZE - 1; |
| 72 | } |
Tejun Heo | 13c589d | 2013-10-01 17:42:02 -0400 | [diff] [blame] | 73 | seq_commit(sf, count); |
| 74 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 77 | static ssize_t sysfs_kf_bin_read(struct kernfs_open_file *of, char *buf, |
Tejun Heo | c2b19da | 2013-11-28 14:54:16 -0500 | [diff] [blame] | 78 | size_t count, loff_t pos) |
Tejun Heo | 2f0c6b7 | 2013-10-01 17:42:06 -0400 | [diff] [blame] | 79 | { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 80 | struct bin_attribute *battr = of->kn->priv; |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 81 | struct kobject *kobj = of->kn->parent->priv; |
Tejun Heo | c2b19da | 2013-11-28 14:54:16 -0500 | [diff] [blame] | 82 | loff_t size = file_inode(of->file)->i_size; |
Tejun Heo | 2f0c6b7 | 2013-10-01 17:42:06 -0400 | [diff] [blame] | 83 | |
Tejun Heo | c2b19da | 2013-11-28 14:54:16 -0500 | [diff] [blame] | 84 | if (!count) |
Tejun Heo | 2f0c6b7 | 2013-10-01 17:42:06 -0400 | [diff] [blame] | 85 | return 0; |
| 86 | |
| 87 | if (size) { |
Vladimir Zapolskiy | eaa5cd9 | 2015-05-22 00:21:16 +0300 | [diff] [blame] | 88 | if (pos >= size) |
Tejun Heo | 2f0c6b7 | 2013-10-01 17:42:06 -0400 | [diff] [blame] | 89 | return 0; |
Tejun Heo | c2b19da | 2013-11-28 14:54:16 -0500 | [diff] [blame] | 90 | if (pos + count > size) |
| 91 | count = size - pos; |
Tejun Heo | 2f0c6b7 | 2013-10-01 17:42:06 -0400 | [diff] [blame] | 92 | } |
| 93 | |
Tejun Heo | c2b19da | 2013-11-28 14:54:16 -0500 | [diff] [blame] | 94 | if (!battr->read) |
| 95 | return -EIO; |
| 96 | |
| 97 | return battr->read(of->file, kobj, battr, buf, pos, count); |
| 98 | } |
| 99 | |
NeilBrown | 4ef67a8 | 2014-10-14 16:57:26 +1100 | [diff] [blame] | 100 | /* kernfs read callback for regular sysfs files with pre-alloc */ |
| 101 | static ssize_t sysfs_kf_read(struct kernfs_open_file *of, char *buf, |
| 102 | size_t count, loff_t pos) |
| 103 | { |
| 104 | const struct sysfs_ops *ops = sysfs_file_ops(of->kn); |
| 105 | struct kobject *kobj = of->kn->parent->priv; |
NeilBrown | c8a139d | 2017-04-03 11:30:34 +1000 | [diff] [blame] | 106 | ssize_t len; |
NeilBrown | 4ef67a8 | 2014-10-14 16:57:26 +1100 | [diff] [blame] | 107 | |
| 108 | /* |
| 109 | * If buf != of->prealloc_buf, we don't know how |
| 110 | * large it is, so cannot safely pass it to ->show |
| 111 | */ |
Konstantin Khlebnikov | 17d0774 | 2016-06-22 21:42:16 +0300 | [diff] [blame] | 112 | if (WARN_ON_ONCE(buf != of->prealloc_buf)) |
NeilBrown | 4ef67a8 | 2014-10-14 16:57:26 +1100 | [diff] [blame] | 113 | return 0; |
NeilBrown | 65da348 | 2015-08-06 08:27:55 +1000 | [diff] [blame] | 114 | len = ops->show(kobj, of->kn->priv, buf); |
NeilBrown | c8a139d | 2017-04-03 11:30:34 +1000 | [diff] [blame] | 115 | if (len < 0) |
| 116 | return len; |
Konstantin Khlebnikov | 17d0774 | 2016-06-22 21:42:16 +0300 | [diff] [blame] | 117 | if (pos) { |
| 118 | if (len <= pos) |
| 119 | return 0; |
| 120 | len -= pos; |
| 121 | memmove(buf, buf + pos, len); |
| 122 | } |
NeilBrown | c8a139d | 2017-04-03 11:30:34 +1000 | [diff] [blame] | 123 | return min_t(ssize_t, count, len); |
NeilBrown | 4ef67a8 | 2014-10-14 16:57:26 +1100 | [diff] [blame] | 124 | } |
| 125 | |
Tejun Heo | 50b38ca | 2013-11-28 14:54:17 -0500 | [diff] [blame] | 126 | /* kernfs write callback for regular sysfs files */ |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 127 | static ssize_t sysfs_kf_write(struct kernfs_open_file *of, char *buf, |
Tejun Heo | 50b38ca | 2013-11-28 14:54:17 -0500 | [diff] [blame] | 128 | size_t count, loff_t pos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 130 | const struct sysfs_ops *ops = sysfs_file_ops(of->kn); |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 131 | struct kobject *kobj = of->kn->parent->priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | |
Tejun Heo | 50b38ca | 2013-11-28 14:54:17 -0500 | [diff] [blame] | 133 | if (!count) |
| 134 | return 0; |
| 135 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 136 | return ops->store(kobj, of->kn->priv, buf, count); |
Tejun Heo | 50b38ca | 2013-11-28 14:54:17 -0500 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | /* kernfs write callback for bin sysfs files */ |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 140 | static ssize_t sysfs_kf_bin_write(struct kernfs_open_file *of, char *buf, |
Tejun Heo | 50b38ca | 2013-11-28 14:54:17 -0500 | [diff] [blame] | 141 | size_t count, loff_t pos) |
| 142 | { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 143 | struct bin_attribute *battr = of->kn->priv; |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 144 | struct kobject *kobj = of->kn->parent->priv; |
Tejun Heo | 50b38ca | 2013-11-28 14:54:17 -0500 | [diff] [blame] | 145 | loff_t size = file_inode(of->file)->i_size; |
| 146 | |
| 147 | if (size) { |
| 148 | if (size <= pos) |
Vladimir Zapolskiy | 0936896 | 2014-09-24 18:21:04 +0300 | [diff] [blame] | 149 | return -EFBIG; |
Tejun Heo | 50b38ca | 2013-11-28 14:54:17 -0500 | [diff] [blame] | 150 | count = min_t(ssize_t, count, size - pos); |
Tejun Heo | 8ef445f | 2013-10-01 17:42:01 -0400 | [diff] [blame] | 151 | } |
Tejun Heo | 50b38ca | 2013-11-28 14:54:17 -0500 | [diff] [blame] | 152 | if (!count) |
| 153 | return 0; |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 154 | |
Tejun Heo | 50b38ca | 2013-11-28 14:54:17 -0500 | [diff] [blame] | 155 | if (!battr->write) |
| 156 | return -EIO; |
Tejun Heo | f9b9a62 | 2013-10-01 17:42:05 -0400 | [diff] [blame] | 157 | |
Tejun Heo | 50b38ca | 2013-11-28 14:54:17 -0500 | [diff] [blame] | 158 | return battr->write(of->file, kobj, battr, buf, pos, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 161 | static int sysfs_kf_bin_mmap(struct kernfs_open_file *of, |
Tejun Heo | fdbffaa | 2013-11-28 14:54:18 -0500 | [diff] [blame] | 162 | struct vm_area_struct *vma) |
| 163 | { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 164 | struct bin_attribute *battr = of->kn->priv; |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 165 | struct kobject *kobj = of->kn->parent->priv; |
Tejun Heo | fdbffaa | 2013-11-28 14:54:18 -0500 | [diff] [blame] | 166 | |
Tejun Heo | fdbffaa | 2013-11-28 14:54:18 -0500 | [diff] [blame] | 167 | return battr->mmap(of->file, kobj, battr, vma); |
| 168 | } |
| 169 | |
Daniel Vetter | 74b3019 | 2020-11-27 17:41:25 +0100 | [diff] [blame] | 170 | static int sysfs_kf_bin_open(struct kernfs_open_file *of) |
| 171 | { |
| 172 | struct bin_attribute *battr = of->kn->priv; |
| 173 | |
Krzysztof Wilczyński | f06aff9 | 2021-07-29 23:32:35 +0000 | [diff] [blame] | 174 | if (battr->f_mapping) |
| 175 | of->file->f_mapping = battr->f_mapping(); |
Daniel Vetter | 74b3019 | 2020-11-27 17:41:25 +0100 | [diff] [blame] | 176 | |
| 177 | return 0; |
| 178 | } |
| 179 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 180 | void sysfs_notify(struct kobject *kobj, const char *dir, const char *attr) |
NeilBrown | 4508a7a | 2006-03-20 17:53:53 +1100 | [diff] [blame] | 181 | { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 182 | struct kernfs_node *kn = kobj->sd, *tmp; |
NeilBrown | 4508a7a | 2006-03-20 17:53:53 +1100 | [diff] [blame] | 183 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 184 | if (kn && dir) |
| 185 | kn = kernfs_find_and_get(kn, dir); |
Tejun Heo | 024f647 | 2013-11-28 14:54:27 -0500 | [diff] [blame] | 186 | else |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 187 | kernfs_get(kn); |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 188 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 189 | if (kn && attr) { |
| 190 | tmp = kernfs_find_and_get(kn, attr); |
| 191 | kernfs_put(kn); |
| 192 | kn = tmp; |
Tejun Heo | 024f647 | 2013-11-28 14:54:27 -0500 | [diff] [blame] | 193 | } |
| 194 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 195 | if (kn) { |
| 196 | kernfs_notify(kn); |
| 197 | kernfs_put(kn); |
Tejun Heo | 024f647 | 2013-11-28 14:54:27 -0500 | [diff] [blame] | 198 | } |
NeilBrown | 4508a7a | 2006-03-20 17:53:53 +1100 | [diff] [blame] | 199 | } |
| 200 | EXPORT_SYMBOL_GPL(sysfs_notify); |
| 201 | |
Tejun Heo | f6acf8b | 2013-11-28 14:54:21 -0500 | [diff] [blame] | 202 | static const struct kernfs_ops sysfs_file_kfops_empty = { |
| 203 | }; |
| 204 | |
| 205 | static const struct kernfs_ops sysfs_file_kfops_ro = { |
| 206 | .seq_show = sysfs_kf_seq_show, |
| 207 | }; |
| 208 | |
| 209 | static const struct kernfs_ops sysfs_file_kfops_wo = { |
| 210 | .write = sysfs_kf_write, |
| 211 | }; |
| 212 | |
| 213 | static const struct kernfs_ops sysfs_file_kfops_rw = { |
| 214 | .seq_show = sysfs_kf_seq_show, |
| 215 | .write = sysfs_kf_write, |
| 216 | }; |
| 217 | |
NeilBrown | 4ef67a8 | 2014-10-14 16:57:26 +1100 | [diff] [blame] | 218 | static const struct kernfs_ops sysfs_prealloc_kfops_ro = { |
| 219 | .read = sysfs_kf_read, |
| 220 | .prealloc = true, |
| 221 | }; |
| 222 | |
NeilBrown | 2b75869 | 2014-10-13 16:41:28 +1100 | [diff] [blame] | 223 | static const struct kernfs_ops sysfs_prealloc_kfops_wo = { |
| 224 | .write = sysfs_kf_write, |
| 225 | .prealloc = true, |
| 226 | }; |
| 227 | |
| 228 | static const struct kernfs_ops sysfs_prealloc_kfops_rw = { |
NeilBrown | 4ef67a8 | 2014-10-14 16:57:26 +1100 | [diff] [blame] | 229 | .read = sysfs_kf_read, |
NeilBrown | 2b75869 | 2014-10-13 16:41:28 +1100 | [diff] [blame] | 230 | .write = sysfs_kf_write, |
| 231 | .prealloc = true, |
| 232 | }; |
| 233 | |
Tejun Heo | f6acf8b | 2013-11-28 14:54:21 -0500 | [diff] [blame] | 234 | static const struct kernfs_ops sysfs_bin_kfops_ro = { |
| 235 | .read = sysfs_kf_bin_read, |
| 236 | }; |
| 237 | |
| 238 | static const struct kernfs_ops sysfs_bin_kfops_wo = { |
| 239 | .write = sysfs_kf_bin_write, |
| 240 | }; |
| 241 | |
| 242 | static const struct kernfs_ops sysfs_bin_kfops_rw = { |
| 243 | .read = sysfs_kf_bin_read, |
| 244 | .write = sysfs_kf_bin_write, |
Tejun Heo | 9b2db6e | 2013-12-10 09:29:17 -0500 | [diff] [blame] | 245 | }; |
| 246 | |
| 247 | static const struct kernfs_ops sysfs_bin_kfops_mmap = { |
| 248 | .read = sysfs_kf_bin_read, |
| 249 | .write = sysfs_kf_bin_write, |
Tejun Heo | f6acf8b | 2013-11-28 14:54:21 -0500 | [diff] [blame] | 250 | .mmap = sysfs_kf_bin_mmap, |
Daniel Vetter | 74b3019 | 2020-11-27 17:41:25 +0100 | [diff] [blame] | 251 | .open = sysfs_kf_bin_open, |
Tejun Heo | f6acf8b | 2013-11-28 14:54:21 -0500 | [diff] [blame] | 252 | }; |
| 253 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 254 | int sysfs_add_file_mode_ns(struct kernfs_node *parent, |
Christoph Hellwig | 5cf3bb0 | 2021-09-13 07:41:12 +0200 | [diff] [blame] | 255 | const struct attribute *attr, umode_t mode, kuid_t uid, |
| 256 | kgid_t gid, const void *ns) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | { |
Christoph Hellwig | 5cf3bb0 | 2021-09-13 07:41:12 +0200 | [diff] [blame] | 258 | struct kobject *kobj = parent->priv; |
| 259 | const struct sysfs_ops *sysfs_ops = kobj->ktype->sysfs_ops; |
Tejun Heo | 517e64f | 2013-11-28 14:54:29 -0500 | [diff] [blame] | 260 | struct lock_class_key *key = NULL; |
Christoph Hellwig | d1a1a96 | 2021-09-13 07:41:13 +0200 | [diff] [blame] | 261 | const struct kernfs_ops *ops = NULL; |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 262 | struct kernfs_node *kn; |
Tejun Heo | a26cd72 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 263 | |
Christoph Hellwig | 5cf3bb0 | 2021-09-13 07:41:12 +0200 | [diff] [blame] | 264 | /* every kobject with an attribute needs a ktype assigned */ |
| 265 | if (WARN(!sysfs_ops, KERN_ERR |
| 266 | "missing sysfs attribute operations for kobject: %s\n", |
| 267 | kobject_name(kobj))) |
| 268 | return -EINVAL; |
Tejun Heo | f6acf8b | 2013-11-28 14:54:21 -0500 | [diff] [blame] | 269 | |
Christoph Hellwig | d1a1a96 | 2021-09-13 07:41:13 +0200 | [diff] [blame] | 270 | if (mode & SYSFS_PREALLOC) { |
| 271 | if (sysfs_ops->show && sysfs_ops->store) |
Christoph Hellwig | 5cf3bb0 | 2021-09-13 07:41:12 +0200 | [diff] [blame] | 272 | ops = &sysfs_prealloc_kfops_rw; |
Christoph Hellwig | d1a1a96 | 2021-09-13 07:41:13 +0200 | [diff] [blame] | 273 | else if (sysfs_ops->show) |
Christoph Hellwig | 5cf3bb0 | 2021-09-13 07:41:12 +0200 | [diff] [blame] | 274 | ops = &sysfs_prealloc_kfops_ro; |
Christoph Hellwig | d1a1a96 | 2021-09-13 07:41:13 +0200 | [diff] [blame] | 275 | else if (sysfs_ops->store) |
Christoph Hellwig | 5cf3bb0 | 2021-09-13 07:41:12 +0200 | [diff] [blame] | 276 | ops = &sysfs_prealloc_kfops_wo; |
Christoph Hellwig | d1a1a96 | 2021-09-13 07:41:13 +0200 | [diff] [blame] | 277 | } else { |
| 278 | if (sysfs_ops->show && sysfs_ops->store) |
| 279 | ops = &sysfs_file_kfops_rw; |
| 280 | else if (sysfs_ops->show) |
| 281 | ops = &sysfs_file_kfops_ro; |
| 282 | else if (sysfs_ops->store) |
Christoph Hellwig | 5cf3bb0 | 2021-09-13 07:41:12 +0200 | [diff] [blame] | 283 | ops = &sysfs_file_kfops_wo; |
Christoph Hellwig | d1a1a96 | 2021-09-13 07:41:13 +0200 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | if (!ops) |
Christoph Hellwig | 5cf3bb0 | 2021-09-13 07:41:12 +0200 | [diff] [blame] | 287 | ops = &sysfs_file_kfops_empty; |
Tejun Heo | f6acf8b | 2013-11-28 14:54:21 -0500 | [diff] [blame] | 288 | |
Tejun Heo | 517e64f | 2013-11-28 14:54:29 -0500 | [diff] [blame] | 289 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 290 | if (!attr->ignore_lockdep) |
| 291 | key = attr->key ?: (struct lock_class_key *)&attr->skey; |
| 292 | #endif |
Dmitry Torokhov | 5f81880 | 2018-07-20 21:56:48 +0000 | [diff] [blame] | 293 | |
| 294 | kn = __kernfs_create_file(parent, attr->name, mode & 0777, uid, gid, |
Christoph Hellwig | 5cf3bb0 | 2021-09-13 07:41:12 +0200 | [diff] [blame] | 295 | PAGE_SIZE, ops, (void *)attr, ns, key); |
| 296 | if (IS_ERR(kn)) { |
| 297 | if (PTR_ERR(kn) == -EEXIST) |
| 298 | sysfs_warn_dup(parent, attr->name); |
| 299 | return PTR_ERR(kn); |
| 300 | } |
| 301 | return 0; |
| 302 | } |
| 303 | |
| 304 | int sysfs_add_bin_file_mode_ns(struct kernfs_node *parent, |
| 305 | const struct bin_attribute *battr, umode_t mode, |
| 306 | kuid_t uid, kgid_t gid, const void *ns) |
| 307 | { |
| 308 | const struct attribute *attr = &battr->attr; |
| 309 | struct lock_class_key *key = NULL; |
| 310 | const struct kernfs_ops *ops; |
| 311 | struct kernfs_node *kn; |
| 312 | |
| 313 | if (battr->mmap) |
| 314 | ops = &sysfs_bin_kfops_mmap; |
| 315 | else if (battr->read && battr->write) |
| 316 | ops = &sysfs_bin_kfops_rw; |
| 317 | else if (battr->read) |
| 318 | ops = &sysfs_bin_kfops_ro; |
| 319 | else if (battr->write) |
| 320 | ops = &sysfs_bin_kfops_wo; |
| 321 | else |
| 322 | ops = &sysfs_file_kfops_empty; |
| 323 | |
| 324 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 325 | if (!attr->ignore_lockdep) |
| 326 | key = attr->key ?: (struct lock_class_key *)&attr->skey; |
| 327 | #endif |
| 328 | |
| 329 | kn = __kernfs_create_file(parent, attr->name, mode & 0777, uid, gid, |
| 330 | battr->size, ops, (void *)attr, ns, key); |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 331 | if (IS_ERR(kn)) { |
| 332 | if (PTR_ERR(kn) == -EEXIST) |
| 333 | sysfs_warn_dup(parent, attr->name); |
| 334 | return PTR_ERR(kn); |
Tejun Heo | 496f739 | 2013-11-28 14:54:24 -0500 | [diff] [blame] | 335 | } |
| 336 | return 0; |
| 337 | } |
| 338 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | /** |
Tejun Heo | 58292cbe | 2013-09-11 22:29:04 -0400 | [diff] [blame] | 340 | * sysfs_create_file_ns - create an attribute file for an object with custom ns |
| 341 | * @kobj: object we're creating for |
| 342 | * @attr: attribute descriptor |
| 343 | * @ns: namespace the new file should belong to |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | */ |
Tejun Heo | 58292cbe | 2013-09-11 22:29:04 -0400 | [diff] [blame] | 345 | int sysfs_create_file_ns(struct kobject *kobj, const struct attribute *attr, |
| 346 | const void *ns) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | { |
Dmitry Torokhov | 5f81880 | 2018-07-20 21:56:48 +0000 | [diff] [blame] | 348 | kuid_t uid; |
| 349 | kgid_t gid; |
| 350 | |
Greg Kroah-Hartman | de96e9f | 2019-01-03 10:23:47 +0100 | [diff] [blame] | 351 | if (WARN_ON(!kobj || !kobj->sd || !attr)) |
| 352 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | |
Dmitry Torokhov | 5f81880 | 2018-07-20 21:56:48 +0000 | [diff] [blame] | 354 | kobject_get_ownership(kobj, &uid, &gid); |
Christoph Hellwig | 5cf3bb0 | 2021-09-13 07:41:12 +0200 | [diff] [blame] | 355 | return sysfs_add_file_mode_ns(kobj->sd, attr, attr->mode, uid, gid, ns); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | } |
Tejun Heo | 58292cbe | 2013-09-11 22:29:04 -0400 | [diff] [blame] | 357 | EXPORT_SYMBOL_GPL(sysfs_create_file_ns); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | |
Jani Nikula | 9ee4685 | 2018-10-04 17:37:49 +0300 | [diff] [blame] | 359 | int sysfs_create_files(struct kobject *kobj, const struct attribute * const *ptr) |
Andi Kleen | 1c205ae | 2010-01-05 12:48:01 +0100 | [diff] [blame] | 360 | { |
| 361 | int err = 0; |
| 362 | int i; |
| 363 | |
| 364 | for (i = 0; ptr[i] && !err; i++) |
| 365 | err = sysfs_create_file(kobj, ptr[i]); |
| 366 | if (err) |
| 367 | while (--i >= 0) |
| 368 | sysfs_remove_file(kobj, ptr[i]); |
| 369 | return err; |
| 370 | } |
Greg Kroah-Hartman | 1b86675 | 2013-08-21 16:17:47 -0700 | [diff] [blame] | 371 | EXPORT_SYMBOL_GPL(sysfs_create_files); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | |
| 373 | /** |
Alan Stern | dfa87c8 | 2007-02-20 15:02:44 -0500 | [diff] [blame] | 374 | * sysfs_add_file_to_group - add an attribute file to a pre-existing group. |
| 375 | * @kobj: object we're acting for. |
| 376 | * @attr: attribute descriptor. |
| 377 | * @group: group name. |
| 378 | */ |
| 379 | int sysfs_add_file_to_group(struct kobject *kobj, |
| 380 | const struct attribute *attr, const char *group) |
| 381 | { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 382 | struct kernfs_node *parent; |
Dmitry Torokhov | 5f81880 | 2018-07-20 21:56:48 +0000 | [diff] [blame] | 383 | kuid_t uid; |
| 384 | kgid_t gid; |
Alan Stern | dfa87c8 | 2007-02-20 15:02:44 -0500 | [diff] [blame] | 385 | int error; |
| 386 | |
Tejun Heo | ccf73cf | 2013-11-28 14:54:30 -0500 | [diff] [blame] | 387 | if (group) { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 388 | parent = kernfs_find_and_get(kobj->sd, group); |
Tejun Heo | ccf73cf | 2013-11-28 14:54:30 -0500 | [diff] [blame] | 389 | } else { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 390 | parent = kobj->sd; |
| 391 | kernfs_get(parent); |
Tejun Heo | ccf73cf | 2013-11-28 14:54:30 -0500 | [diff] [blame] | 392 | } |
James Bottomley | 11f24fb | 2008-01-02 18:44:05 -0600 | [diff] [blame] | 393 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 394 | if (!parent) |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 395 | return -ENOENT; |
| 396 | |
Dmitry Torokhov | 5f81880 | 2018-07-20 21:56:48 +0000 | [diff] [blame] | 397 | kobject_get_ownership(kobj, &uid, &gid); |
Christoph Hellwig | 5cf3bb0 | 2021-09-13 07:41:12 +0200 | [diff] [blame] | 398 | error = sysfs_add_file_mode_ns(parent, attr, attr->mode, uid, gid, |
| 399 | NULL); |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 400 | kernfs_put(parent); |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 401 | |
Alan Stern | dfa87c8 | 2007-02-20 15:02:44 -0500 | [diff] [blame] | 402 | return error; |
| 403 | } |
| 404 | EXPORT_SYMBOL_GPL(sysfs_add_file_to_group); |
| 405 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | /** |
Kay Sievers | 31e5abe | 2005-04-18 21:57:32 -0700 | [diff] [blame] | 407 | * sysfs_chmod_file - update the modified mode value on an object attribute. |
| 408 | * @kobj: object we're acting for. |
| 409 | * @attr: attribute descriptor. |
| 410 | * @mode: file permissions. |
| 411 | * |
| 412 | */ |
Jean Delvare | 49c1940 | 2010-07-02 16:54:05 +0200 | [diff] [blame] | 413 | int sysfs_chmod_file(struct kobject *kobj, const struct attribute *attr, |
Al Viro | 48176a9 | 2011-07-24 03:40:40 -0400 | [diff] [blame] | 414 | umode_t mode) |
Kay Sievers | 31e5abe | 2005-04-18 21:57:32 -0700 | [diff] [blame] | 415 | { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 416 | struct kernfs_node *kn; |
Maneesh Soni | bc062b1 | 2005-07-29 12:13:35 -0700 | [diff] [blame] | 417 | struct iattr newattrs; |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 418 | int rc; |
Kay Sievers | 31e5abe | 2005-04-18 21:57:32 -0700 | [diff] [blame] | 419 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 420 | kn = kernfs_find_and_get(kobj->sd, attr->name); |
| 421 | if (!kn) |
Tejun Heo | 5d60418 | 2013-11-23 17:21:52 -0500 | [diff] [blame] | 422 | return -ENOENT; |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 423 | |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 424 | newattrs.ia_mode = (mode & S_IALLUGO) | (kn->mode & ~S_IALLUGO); |
Eric W. Biederman | 4c6974f | 2009-11-07 23:27:02 -0800 | [diff] [blame] | 425 | newattrs.ia_valid = ATTR_MODE; |
Tejun Heo | f88123e | 2007-09-20 16:05:10 +0900 | [diff] [blame] | 426 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 427 | rc = kernfs_setattr(kn, &newattrs); |
Tejun Heo | 5d60418 | 2013-11-23 17:21:52 -0500 | [diff] [blame] | 428 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 429 | kernfs_put(kn); |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 430 | return rc; |
Kay Sievers | 31e5abe | 2005-04-18 21:57:32 -0700 | [diff] [blame] | 431 | } |
| 432 | EXPORT_SYMBOL_GPL(sysfs_chmod_file); |
| 433 | |
Kay Sievers | 31e5abe | 2005-04-18 21:57:32 -0700 | [diff] [blame] | 434 | /** |
Bart Van Assche | 2afc916 | 2018-08-02 10:51:40 -0700 | [diff] [blame] | 435 | * sysfs_break_active_protection - break "active" protection |
| 436 | * @kobj: The kernel object @attr is associated with. |
| 437 | * @attr: The attribute to break the "active" protection for. |
| 438 | * |
| 439 | * With sysfs, just like kernfs, deletion of an attribute is postponed until |
| 440 | * all active .show() and .store() callbacks have finished unless this function |
| 441 | * is called. Hence this function is useful in methods that implement self |
| 442 | * deletion. |
| 443 | */ |
| 444 | struct kernfs_node *sysfs_break_active_protection(struct kobject *kobj, |
| 445 | const struct attribute *attr) |
| 446 | { |
| 447 | struct kernfs_node *kn; |
| 448 | |
| 449 | kobject_get(kobj); |
| 450 | kn = kernfs_find_and_get(kobj->sd, attr->name); |
| 451 | if (kn) |
| 452 | kernfs_break_active_protection(kn); |
| 453 | return kn; |
| 454 | } |
| 455 | EXPORT_SYMBOL_GPL(sysfs_break_active_protection); |
| 456 | |
| 457 | /** |
| 458 | * sysfs_unbreak_active_protection - restore "active" protection |
| 459 | * @kn: Pointer returned by sysfs_break_active_protection(). |
| 460 | * |
| 461 | * Undo the effects of sysfs_break_active_protection(). Since this function |
| 462 | * calls kernfs_put() on the kernfs node that corresponds to the 'attr' |
| 463 | * argument passed to sysfs_break_active_protection() that attribute may have |
| 464 | * been removed between the sysfs_break_active_protection() and |
| 465 | * sysfs_unbreak_active_protection() calls, it is not safe to access @kn after |
| 466 | * this function has returned. |
| 467 | */ |
| 468 | void sysfs_unbreak_active_protection(struct kernfs_node *kn) |
| 469 | { |
| 470 | struct kobject *kobj = kn->parent->priv; |
| 471 | |
| 472 | kernfs_unbreak_active_protection(kn); |
| 473 | kernfs_put(kn); |
| 474 | kobject_put(kobj); |
| 475 | } |
| 476 | EXPORT_SYMBOL_GPL(sysfs_unbreak_active_protection); |
| 477 | |
| 478 | /** |
Tejun Heo | 58292cbe | 2013-09-11 22:29:04 -0400 | [diff] [blame] | 479 | * sysfs_remove_file_ns - remove an object attribute with a custom ns tag |
| 480 | * @kobj: object we're acting for |
| 481 | * @attr: attribute descriptor |
| 482 | * @ns: namespace tag of the file to remove |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | * |
Tejun Heo | 58292cbe | 2013-09-11 22:29:04 -0400 | [diff] [blame] | 484 | * Hash the attribute name and namespace tag and kill the victim. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | */ |
Tejun Heo | 58292cbe | 2013-09-11 22:29:04 -0400 | [diff] [blame] | 486 | void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr, |
| 487 | const void *ns) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 489 | struct kernfs_node *parent = kobj->sd; |
Eric W. Biederman | 487505c | 2011-10-12 21:53:38 +0000 | [diff] [blame] | 490 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 491 | kernfs_remove_by_name_ns(parent, attr->name, ns); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | } |
Tejun Heo | 58292cbe | 2013-09-11 22:29:04 -0400 | [diff] [blame] | 493 | EXPORT_SYMBOL_GPL(sysfs_remove_file_ns); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | |
Tejun Heo | 6b0afc2 | 2014-02-03 14:03:01 -0500 | [diff] [blame] | 495 | /** |
| 496 | * sysfs_remove_file_self - remove an object attribute from its own method |
| 497 | * @kobj: object we're acting for |
| 498 | * @attr: attribute descriptor |
| 499 | * |
| 500 | * See kernfs_remove_self() for details. |
| 501 | */ |
| 502 | bool sysfs_remove_file_self(struct kobject *kobj, const struct attribute *attr) |
| 503 | { |
| 504 | struct kernfs_node *parent = kobj->sd; |
| 505 | struct kernfs_node *kn; |
| 506 | bool ret; |
| 507 | |
| 508 | kn = kernfs_find_and_get(parent, attr->name); |
| 509 | if (WARN_ON_ONCE(!kn)) |
| 510 | return false; |
| 511 | |
| 512 | ret = kernfs_remove_self(kn); |
| 513 | |
| 514 | kernfs_put(kn); |
| 515 | return ret; |
| 516 | } |
Jack Wang | 9ddacff | 2020-05-11 15:51:07 +0200 | [diff] [blame] | 517 | EXPORT_SYMBOL_GPL(sysfs_remove_file_self); |
Tejun Heo | 6b0afc2 | 2014-02-03 14:03:01 -0500 | [diff] [blame] | 518 | |
Jani Nikula | 9ee4685 | 2018-10-04 17:37:49 +0300 | [diff] [blame] | 519 | void sysfs_remove_files(struct kobject *kobj, const struct attribute * const *ptr) |
Andi Kleen | 1c205ae | 2010-01-05 12:48:01 +0100 | [diff] [blame] | 520 | { |
| 521 | int i; |
Stephen Martin | 4bd4e92 | 2018-12-20 13:50:28 -0800 | [diff] [blame] | 522 | |
Andi Kleen | 1c205ae | 2010-01-05 12:48:01 +0100 | [diff] [blame] | 523 | for (i = 0; ptr[i]; i++) |
| 524 | sysfs_remove_file(kobj, ptr[i]); |
| 525 | } |
Greg Kroah-Hartman | 1b86675 | 2013-08-21 16:17:47 -0700 | [diff] [blame] | 526 | EXPORT_SYMBOL_GPL(sysfs_remove_files); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | |
Alan Stern | dfa87c8 | 2007-02-20 15:02:44 -0500 | [diff] [blame] | 528 | /** |
| 529 | * sysfs_remove_file_from_group - remove an attribute file from a group. |
| 530 | * @kobj: object we're acting for. |
| 531 | * @attr: attribute descriptor. |
| 532 | * @group: group name. |
| 533 | */ |
| 534 | void sysfs_remove_file_from_group(struct kobject *kobj, |
| 535 | const struct attribute *attr, const char *group) |
| 536 | { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 537 | struct kernfs_node *parent; |
Alan Stern | dfa87c8 | 2007-02-20 15:02:44 -0500 | [diff] [blame] | 538 | |
Tejun Heo | ccf73cf | 2013-11-28 14:54:30 -0500 | [diff] [blame] | 539 | if (group) { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 540 | parent = kernfs_find_and_get(kobj->sd, group); |
Tejun Heo | ccf73cf | 2013-11-28 14:54:30 -0500 | [diff] [blame] | 541 | } else { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 542 | parent = kobj->sd; |
| 543 | kernfs_get(parent); |
Tejun Heo | ccf73cf | 2013-11-28 14:54:30 -0500 | [diff] [blame] | 544 | } |
| 545 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 546 | if (parent) { |
| 547 | kernfs_remove_by_name(parent, attr->name); |
| 548 | kernfs_put(parent); |
Alan Stern | dfa87c8 | 2007-02-20 15:02:44 -0500 | [diff] [blame] | 549 | } |
| 550 | } |
| 551 | EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group); |
| 552 | |
Tejun Heo | 3124eb1 | 2013-10-01 17:42:09 -0400 | [diff] [blame] | 553 | /** |
| 554 | * sysfs_create_bin_file - create binary file for object. |
| 555 | * @kobj: object. |
| 556 | * @attr: attribute descriptor. |
| 557 | */ |
| 558 | int sysfs_create_bin_file(struct kobject *kobj, |
| 559 | const struct bin_attribute *attr) |
| 560 | { |
Dmitry Torokhov | 5f81880 | 2018-07-20 21:56:48 +0000 | [diff] [blame] | 561 | kuid_t uid; |
| 562 | kgid_t gid; |
| 563 | |
Greg Kroah-Hartman | de96e9f | 2019-01-03 10:23:47 +0100 | [diff] [blame] | 564 | if (WARN_ON(!kobj || !kobj->sd || !attr)) |
| 565 | return -EINVAL; |
Tejun Heo | 3124eb1 | 2013-10-01 17:42:09 -0400 | [diff] [blame] | 566 | |
Dmitry Torokhov | 5f81880 | 2018-07-20 21:56:48 +0000 | [diff] [blame] | 567 | kobject_get_ownership(kobj, &uid, &gid); |
Christoph Hellwig | 5cf3bb0 | 2021-09-13 07:41:12 +0200 | [diff] [blame] | 568 | return sysfs_add_bin_file_mode_ns(kobj->sd, attr, attr->attr.mode, uid, |
| 569 | gid, NULL); |
Tejun Heo | 3124eb1 | 2013-10-01 17:42:09 -0400 | [diff] [blame] | 570 | } |
| 571 | EXPORT_SYMBOL_GPL(sysfs_create_bin_file); |
| 572 | |
| 573 | /** |
| 574 | * sysfs_remove_bin_file - remove binary file for object. |
| 575 | * @kobj: object. |
| 576 | * @attr: attribute descriptor. |
| 577 | */ |
| 578 | void sysfs_remove_bin_file(struct kobject *kobj, |
| 579 | const struct bin_attribute *attr) |
| 580 | { |
Tejun Heo | 879f40d | 2013-11-23 17:21:49 -0500 | [diff] [blame] | 581 | kernfs_remove_by_name(kobj->sd, attr->attr.name); |
Tejun Heo | 3124eb1 | 2013-10-01 17:42:09 -0400 | [diff] [blame] | 582 | } |
| 583 | EXPORT_SYMBOL_GPL(sysfs_remove_bin_file); |
Christian Brauner | f70ce18 | 2020-02-27 04:37:11 +0100 | [diff] [blame] | 584 | |
| 585 | static int internal_change_owner(struct kernfs_node *kn, kuid_t kuid, |
| 586 | kgid_t kgid) |
| 587 | { |
| 588 | struct iattr newattrs = { |
| 589 | .ia_valid = ATTR_UID | ATTR_GID, |
| 590 | .ia_uid = kuid, |
| 591 | .ia_gid = kgid, |
| 592 | }; |
| 593 | return kernfs_setattr(kn, &newattrs); |
| 594 | } |
| 595 | |
| 596 | /** |
Christian Brauner | 0666a3a | 2020-02-27 04:37:12 +0100 | [diff] [blame] | 597 | * sysfs_link_change_owner - change owner of a sysfs file. |
| 598 | * @kobj: object of the kernfs_node the symlink is located in. |
| 599 | * @targ: object of the kernfs_node the symlink points to. |
| 600 | * @name: name of the link. |
| 601 | * @kuid: new owner's kuid |
| 602 | * @kgid: new owner's kgid |
| 603 | * |
| 604 | * This function looks up the sysfs symlink entry @name under @kobj and changes |
| 605 | * the ownership to @kuid/@kgid. The symlink is looked up in the namespace of |
| 606 | * @targ. |
| 607 | * |
| 608 | * Returns 0 on success or error code on failure. |
| 609 | */ |
| 610 | int sysfs_link_change_owner(struct kobject *kobj, struct kobject *targ, |
| 611 | const char *name, kuid_t kuid, kgid_t kgid) |
| 612 | { |
| 613 | struct kernfs_node *kn = NULL; |
| 614 | int error; |
| 615 | |
| 616 | if (!name || !kobj->state_in_sysfs || !targ->state_in_sysfs) |
| 617 | return -EINVAL; |
| 618 | |
| 619 | error = -ENOENT; |
| 620 | kn = kernfs_find_and_get_ns(kobj->sd, name, targ->sd->ns); |
| 621 | if (!kn) |
| 622 | goto out; |
| 623 | |
| 624 | error = -EINVAL; |
| 625 | if (kernfs_type(kn) != KERNFS_LINK) |
| 626 | goto out; |
| 627 | if (kn->symlink.target_kn->priv != targ) |
| 628 | goto out; |
| 629 | |
| 630 | error = internal_change_owner(kn, kuid, kgid); |
| 631 | |
| 632 | out: |
| 633 | kernfs_put(kn); |
| 634 | return error; |
| 635 | } |
| 636 | |
| 637 | /** |
Christian Brauner | f70ce18 | 2020-02-27 04:37:11 +0100 | [diff] [blame] | 638 | * sysfs_file_change_owner - change owner of a sysfs file. |
| 639 | * @kobj: object. |
| 640 | * @name: name of the file to change. |
| 641 | * @kuid: new owner's kuid |
| 642 | * @kgid: new owner's kgid |
| 643 | * |
| 644 | * This function looks up the sysfs entry @name under @kobj and changes the |
| 645 | * ownership to @kuid/@kgid. |
| 646 | * |
| 647 | * Returns 0 on success or error code on failure. |
| 648 | */ |
| 649 | int sysfs_file_change_owner(struct kobject *kobj, const char *name, kuid_t kuid, |
| 650 | kgid_t kgid) |
| 651 | { |
| 652 | struct kernfs_node *kn; |
| 653 | int error; |
| 654 | |
| 655 | if (!name) |
| 656 | return -EINVAL; |
| 657 | |
| 658 | if (!kobj->state_in_sysfs) |
| 659 | return -EINVAL; |
| 660 | |
| 661 | kn = kernfs_find_and_get(kobj->sd, name); |
| 662 | if (!kn) |
| 663 | return -ENOENT; |
| 664 | |
| 665 | error = internal_change_owner(kn, kuid, kgid); |
| 666 | |
| 667 | kernfs_put(kn); |
| 668 | |
| 669 | return error; |
| 670 | } |
| 671 | EXPORT_SYMBOL_GPL(sysfs_file_change_owner); |
Christian Brauner | 2c4f940 | 2020-02-27 04:37:14 +0100 | [diff] [blame] | 672 | |
| 673 | /** |
| 674 | * sysfs_change_owner - change owner of the given object. |
| 675 | * @kobj: object. |
| 676 | * @kuid: new owner's kuid |
| 677 | * @kgid: new owner's kgid |
| 678 | * |
| 679 | * Change the owner of the default directory, files, groups, and attributes of |
| 680 | * @kobj to @kuid/@kgid. Note that sysfs_change_owner mirrors how the sysfs |
| 681 | * entries for a kobject are added by driver core. In summary, |
| 682 | * sysfs_change_owner() takes care of the default directory entry for @kobj, |
| 683 | * the default attributes associated with the ktype of @kobj and the default |
| 684 | * attributes associated with the ktype of @kobj. |
| 685 | * Additional properties not added by driver core have to be changed by the |
| 686 | * driver or subsystem which created them. This is similar to how |
| 687 | * driver/subsystem specific entries are removed. |
| 688 | * |
| 689 | * Returns 0 on success or error code on failure. |
| 690 | */ |
| 691 | int sysfs_change_owner(struct kobject *kobj, kuid_t kuid, kgid_t kgid) |
| 692 | { |
| 693 | int error; |
| 694 | const struct kobj_type *ktype; |
| 695 | |
| 696 | if (!kobj->state_in_sysfs) |
| 697 | return -EINVAL; |
| 698 | |
| 699 | /* Change the owner of the kobject itself. */ |
| 700 | error = internal_change_owner(kobj->sd, kuid, kgid); |
| 701 | if (error) |
| 702 | return error; |
| 703 | |
| 704 | ktype = get_ktype(kobj); |
| 705 | if (ktype) { |
| 706 | struct attribute **kattr; |
| 707 | |
| 708 | /* |
| 709 | * Change owner of the default attributes associated with the |
| 710 | * ktype of @kobj. |
| 711 | */ |
| 712 | for (kattr = ktype->default_attrs; kattr && *kattr; kattr++) { |
| 713 | error = sysfs_file_change_owner(kobj, (*kattr)->name, |
| 714 | kuid, kgid); |
| 715 | if (error) |
| 716 | return error; |
| 717 | } |
| 718 | |
| 719 | /* |
| 720 | * Change owner of the default groups associated with the |
| 721 | * ktype of @kobj. |
| 722 | */ |
| 723 | error = sysfs_groups_change_owner(kobj, ktype->default_groups, |
| 724 | kuid, kgid); |
| 725 | if (error) |
| 726 | return error; |
| 727 | } |
| 728 | |
| 729 | return 0; |
| 730 | } |
| 731 | EXPORT_SYMBOL_GPL(sysfs_change_owner); |
Joe Perches | 2efc459 | 2020-09-16 13:40:38 -0700 | [diff] [blame] | 732 | |
| 733 | /** |
| 734 | * sysfs_emit - scnprintf equivalent, aware of PAGE_SIZE buffer. |
| 735 | * @buf: start of PAGE_SIZE buffer. |
| 736 | * @fmt: format |
| 737 | * @...: optional arguments to @format |
| 738 | * |
| 739 | * |
| 740 | * Returns number of characters written to @buf. |
| 741 | */ |
| 742 | int sysfs_emit(char *buf, const char *fmt, ...) |
| 743 | { |
| 744 | va_list args; |
| 745 | int len; |
| 746 | |
| 747 | if (WARN(!buf || offset_in_page(buf), |
| 748 | "invalid sysfs_emit: buf:%p\n", buf)) |
| 749 | return 0; |
| 750 | |
| 751 | va_start(args, fmt); |
| 752 | len = vscnprintf(buf, PAGE_SIZE, fmt, args); |
| 753 | va_end(args); |
| 754 | |
| 755 | return len; |
| 756 | } |
| 757 | EXPORT_SYMBOL_GPL(sysfs_emit); |
| 758 | |
| 759 | /** |
| 760 | * sysfs_emit_at - scnprintf equivalent, aware of PAGE_SIZE buffer. |
| 761 | * @buf: start of PAGE_SIZE buffer. |
| 762 | * @at: offset in @buf to start write in bytes |
| 763 | * @at must be >= 0 && < PAGE_SIZE |
| 764 | * @fmt: format |
| 765 | * @...: optional arguments to @fmt |
| 766 | * |
| 767 | * |
| 768 | * Returns number of characters written starting at &@buf[@at]. |
| 769 | */ |
| 770 | int sysfs_emit_at(char *buf, int at, const char *fmt, ...) |
| 771 | { |
| 772 | va_list args; |
| 773 | int len; |
| 774 | |
| 775 | if (WARN(!buf || offset_in_page(buf) || at < 0 || at >= PAGE_SIZE, |
| 776 | "invalid sysfs_emit_at: buf:%p at:%d\n", buf, at)) |
| 777 | return 0; |
| 778 | |
| 779 | va_start(args, fmt); |
| 780 | len = vscnprintf(buf + at, PAGE_SIZE - at, fmt, args); |
| 781 | va_end(args); |
| 782 | |
| 783 | return len; |
| 784 | } |
| 785 | EXPORT_SYMBOL_GPL(sysfs_emit_at); |