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