blob: 42dcf96881b688f3474a76bb6b618eade3c2b959 [file] [log] [blame]
Greg Kroah-Hartman619daee2018-01-22 16:18:13 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Tejun Heo6d66f5c2007-09-20 17:31:38 +09003 * 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 Chehab0c1bc6b2020-04-14 18:48:37 +02009 * Please see Documentation/filesystems/sysfs.rst for more information.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11
12#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/kobject.h>
Robert P. J. Dayc6f87732008-03-13 22:41:52 -040014#include <linux/slab.h>
Oliver Neukum94bebf42006-12-20 10:52:44 +010015#include <linux/list.h>
Dave Young52e8c202007-07-26 11:03:54 +000016#include <linux/mutex.h>
Tejun Heo13c589d2013-10-01 17:42:02 -040017#include <linux/seq_file.h>
Joe Perches2efc4592020-09-16 13:40:38 -070018#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20#include "sysfs.h"
Tejun Heof6acf8b2013-11-28 14:54:21 -050021
22/*
Tejun Heo324a56e2013-12-11 14:11:53 -050023 * Determine ktype->sysfs_ops for the given kernfs_node. This function
Tejun Heo375b6112013-10-01 17:41:57 -040024 * must be called while holding an active reference.
25 */
Tejun Heo324a56e2013-12-11 14:11:53 -050026static const struct sysfs_ops *sysfs_file_ops(struct kernfs_node *kn)
Tejun Heo375b6112013-10-01 17:41:57 -040027{
Tejun Heoadc5e8b2013-12-11 14:11:54 -050028 struct kobject *kobj = kn->parent->priv;
Tejun Heo375b6112013-10-01 17:41:57 -040029
Tejun Heodf23fc32013-12-11 14:11:56 -050030 if (kn->flags & KERNFS_LOCKDEP)
Tejun Heo324a56e2013-12-11 14:11:53 -050031 lockdep_assert_held(kn);
Tejun Heo375b6112013-10-01 17:41:57 -040032 return kobj->ktype ? kobj->ktype->sysfs_ops : NULL;
33}
34
Tejun Heo13c589d2013-10-01 17:42:02 -040035/*
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 Torvalds1da177e2005-04-16 15:20:36 -070039 */
Tejun Heoc2b19da2013-11-28 14:54:16 -050040static int sysfs_kf_seq_show(struct seq_file *sf, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
Tejun Heoc525aad2013-12-11 14:11:55 -050042 struct kernfs_open_file *of = sf->private;
Tejun Heoadc5e8b2013-12-11 14:11:54 -050043 struct kobject *kobj = of->kn->parent->priv;
Tejun Heo324a56e2013-12-11 14:11:53 -050044 const struct sysfs_ops *ops = sysfs_file_ops(of->kn);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 ssize_t count;
Tejun Heoc2b19da2013-11-28 14:54:16 -050046 char *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Christoph Hellwig820879e2021-09-13 07:41:14 +020048 if (WARN_ON_ONCE(!ops->show))
49 return -EINVAL;
50
Tejun Heof5c16f22014-05-19 15:52:10 -040051 /* acquire buffer and ensure that it's >= PAGE_SIZE and clear */
Tejun Heo13c589d2013-10-01 17:42:02 -040052 count = seq_get_buf(sf, &buf);
53 if (count < PAGE_SIZE) {
54 seq_commit(sf, -1);
55 return 0;
56 }
Tejun Heof5c16f22014-05-19 15:52:10 -040057 memset(buf, 0, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Christoph Hellwig820879e2021-09-13 07:41:14 +020059 count = ops->show(kobj, of->kn->priv, buf);
60 if (count < 0)
61 return count;
Tejun Heo0ab66082007-06-14 03:45:16 +090062
Miao Xie8118a852007-11-21 14:55:19 -080063 /*
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 Morton815d2d52008-03-04 15:09:07 -080067 if (count >= (ssize_t)PAGE_SIZE) {
Sergey Senozhatsky9e6d35f2017-12-11 21:50:22 +090068 printk("fill_read_buffer: %pS returned bad count\n",
69 ops->show);
Andrew Morton815d2d52008-03-04 15:09:07 -080070 /* Try to struggle along */
71 count = PAGE_SIZE - 1;
72 }
Tejun Heo13c589d2013-10-01 17:42:02 -040073 seq_commit(sf, count);
74 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075}
76
Tejun Heoc525aad2013-12-11 14:11:55 -050077static ssize_t sysfs_kf_bin_read(struct kernfs_open_file *of, char *buf,
Tejun Heoc2b19da2013-11-28 14:54:16 -050078 size_t count, loff_t pos)
Tejun Heo2f0c6b72013-10-01 17:42:06 -040079{
Tejun Heo324a56e2013-12-11 14:11:53 -050080 struct bin_attribute *battr = of->kn->priv;
Tejun Heoadc5e8b2013-12-11 14:11:54 -050081 struct kobject *kobj = of->kn->parent->priv;
Tejun Heoc2b19da2013-11-28 14:54:16 -050082 loff_t size = file_inode(of->file)->i_size;
Tejun Heo2f0c6b72013-10-01 17:42:06 -040083
Tejun Heoc2b19da2013-11-28 14:54:16 -050084 if (!count)
Tejun Heo2f0c6b72013-10-01 17:42:06 -040085 return 0;
86
87 if (size) {
Vladimir Zapolskiyeaa5cd92015-05-22 00:21:16 +030088 if (pos >= size)
Tejun Heo2f0c6b72013-10-01 17:42:06 -040089 return 0;
Tejun Heoc2b19da2013-11-28 14:54:16 -050090 if (pos + count > size)
91 count = size - pos;
Tejun Heo2f0c6b72013-10-01 17:42:06 -040092 }
93
Tejun Heoc2b19da2013-11-28 14:54:16 -050094 if (!battr->read)
95 return -EIO;
96
97 return battr->read(of->file, kobj, battr, buf, pos, count);
98}
99
NeilBrown4ef67a82014-10-14 16:57:26 +1100100/* kernfs read callback for regular sysfs files with pre-alloc */
101static 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;
NeilBrownc8a139d2017-04-03 11:30:34 +1000106 ssize_t len;
NeilBrown4ef67a82014-10-14 16:57:26 +1100107
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 Khlebnikov17d07742016-06-22 21:42:16 +0300112 if (WARN_ON_ONCE(buf != of->prealloc_buf))
NeilBrown4ef67a82014-10-14 16:57:26 +1100113 return 0;
NeilBrown65da3482015-08-06 08:27:55 +1000114 len = ops->show(kobj, of->kn->priv, buf);
NeilBrownc8a139d2017-04-03 11:30:34 +1000115 if (len < 0)
116 return len;
Konstantin Khlebnikov17d07742016-06-22 21:42:16 +0300117 if (pos) {
118 if (len <= pos)
119 return 0;
120 len -= pos;
121 memmove(buf, buf + pos, len);
122 }
NeilBrownc8a139d2017-04-03 11:30:34 +1000123 return min_t(ssize_t, count, len);
NeilBrown4ef67a82014-10-14 16:57:26 +1100124}
125
Tejun Heo50b38ca2013-11-28 14:54:17 -0500126/* kernfs write callback for regular sysfs files */
Tejun Heoc525aad2013-12-11 14:11:55 -0500127static ssize_t sysfs_kf_write(struct kernfs_open_file *of, char *buf,
Tejun Heo50b38ca2013-11-28 14:54:17 -0500128 size_t count, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
Tejun Heo324a56e2013-12-11 14:11:53 -0500130 const struct sysfs_ops *ops = sysfs_file_ops(of->kn);
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500131 struct kobject *kobj = of->kn->parent->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Tejun Heo50b38ca2013-11-28 14:54:17 -0500133 if (!count)
134 return 0;
135
Tejun Heo324a56e2013-12-11 14:11:53 -0500136 return ops->store(kobj, of->kn->priv, buf, count);
Tejun Heo50b38ca2013-11-28 14:54:17 -0500137}
138
139/* kernfs write callback for bin sysfs files */
Tejun Heoc525aad2013-12-11 14:11:55 -0500140static ssize_t sysfs_kf_bin_write(struct kernfs_open_file *of, char *buf,
Tejun Heo50b38ca2013-11-28 14:54:17 -0500141 size_t count, loff_t pos)
142{
Tejun Heo324a56e2013-12-11 14:11:53 -0500143 struct bin_attribute *battr = of->kn->priv;
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500144 struct kobject *kobj = of->kn->parent->priv;
Tejun Heo50b38ca2013-11-28 14:54:17 -0500145 loff_t size = file_inode(of->file)->i_size;
146
147 if (size) {
148 if (size <= pos)
Vladimir Zapolskiy09368962014-09-24 18:21:04 +0300149 return -EFBIG;
Tejun Heo50b38ca2013-11-28 14:54:17 -0500150 count = min_t(ssize_t, count, size - pos);
Tejun Heo8ef445f2013-10-01 17:42:01 -0400151 }
Tejun Heo50b38ca2013-11-28 14:54:17 -0500152 if (!count)
153 return 0;
Tejun Heo0ab66082007-06-14 03:45:16 +0900154
Tejun Heo50b38ca2013-11-28 14:54:17 -0500155 if (!battr->write)
156 return -EIO;
Tejun Heof9b9a622013-10-01 17:42:05 -0400157
Tejun Heo50b38ca2013-11-28 14:54:17 -0500158 return battr->write(of->file, kobj, battr, buf, pos, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160
Tejun Heoc525aad2013-12-11 14:11:55 -0500161static int sysfs_kf_bin_mmap(struct kernfs_open_file *of,
Tejun Heofdbffaa2013-11-28 14:54:18 -0500162 struct vm_area_struct *vma)
163{
Tejun Heo324a56e2013-12-11 14:11:53 -0500164 struct bin_attribute *battr = of->kn->priv;
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500165 struct kobject *kobj = of->kn->parent->priv;
Tejun Heofdbffaa2013-11-28 14:54:18 -0500166
Tejun Heofdbffaa2013-11-28 14:54:18 -0500167 return battr->mmap(of->file, kobj, battr, vma);
168}
169
Daniel Vetter74b30192020-11-27 17:41:25 +0100170static int sysfs_kf_bin_open(struct kernfs_open_file *of)
171{
172 struct bin_attribute *battr = of->kn->priv;
173
Krzysztof Wilczyńskif06aff92021-07-29 23:32:35 +0000174 if (battr->f_mapping)
175 of->file->f_mapping = battr->f_mapping();
Daniel Vetter74b30192020-11-27 17:41:25 +0100176
177 return 0;
178}
179
Tejun Heo324a56e2013-12-11 14:11:53 -0500180void sysfs_notify(struct kobject *kobj, const char *dir, const char *attr)
NeilBrown4508a7a2006-03-20 17:53:53 +1100181{
Tejun Heo324a56e2013-12-11 14:11:53 -0500182 struct kernfs_node *kn = kobj->sd, *tmp;
NeilBrown4508a7a2006-03-20 17:53:53 +1100183
Tejun Heo324a56e2013-12-11 14:11:53 -0500184 if (kn && dir)
185 kn = kernfs_find_and_get(kn, dir);
Tejun Heo024f6472013-11-28 14:54:27 -0500186 else
Tejun Heo324a56e2013-12-11 14:11:53 -0500187 kernfs_get(kn);
Tejun Heo51225032007-06-14 04:27:25 +0900188
Tejun Heo324a56e2013-12-11 14:11:53 -0500189 if (kn && attr) {
190 tmp = kernfs_find_and_get(kn, attr);
191 kernfs_put(kn);
192 kn = tmp;
Tejun Heo024f6472013-11-28 14:54:27 -0500193 }
194
Tejun Heo324a56e2013-12-11 14:11:53 -0500195 if (kn) {
196 kernfs_notify(kn);
197 kernfs_put(kn);
Tejun Heo024f6472013-11-28 14:54:27 -0500198 }
NeilBrown4508a7a2006-03-20 17:53:53 +1100199}
200EXPORT_SYMBOL_GPL(sysfs_notify);
201
Tejun Heof6acf8b2013-11-28 14:54:21 -0500202static const struct kernfs_ops sysfs_file_kfops_empty = {
203};
204
205static const struct kernfs_ops sysfs_file_kfops_ro = {
206 .seq_show = sysfs_kf_seq_show,
207};
208
209static const struct kernfs_ops sysfs_file_kfops_wo = {
210 .write = sysfs_kf_write,
211};
212
213static const struct kernfs_ops sysfs_file_kfops_rw = {
214 .seq_show = sysfs_kf_seq_show,
215 .write = sysfs_kf_write,
216};
217
NeilBrown4ef67a82014-10-14 16:57:26 +1100218static const struct kernfs_ops sysfs_prealloc_kfops_ro = {
219 .read = sysfs_kf_read,
220 .prealloc = true,
221};
222
NeilBrown2b758692014-10-13 16:41:28 +1100223static const struct kernfs_ops sysfs_prealloc_kfops_wo = {
224 .write = sysfs_kf_write,
225 .prealloc = true,
226};
227
228static const struct kernfs_ops sysfs_prealloc_kfops_rw = {
NeilBrown4ef67a82014-10-14 16:57:26 +1100229 .read = sysfs_kf_read,
NeilBrown2b758692014-10-13 16:41:28 +1100230 .write = sysfs_kf_write,
231 .prealloc = true,
232};
233
Tejun Heof6acf8b2013-11-28 14:54:21 -0500234static const struct kernfs_ops sysfs_bin_kfops_ro = {
235 .read = sysfs_kf_bin_read,
236};
237
238static const struct kernfs_ops sysfs_bin_kfops_wo = {
239 .write = sysfs_kf_bin_write,
240};
241
242static const struct kernfs_ops sysfs_bin_kfops_rw = {
243 .read = sysfs_kf_bin_read,
244 .write = sysfs_kf_bin_write,
Tejun Heo9b2db6e2013-12-10 09:29:17 -0500245};
246
247static const struct kernfs_ops sysfs_bin_kfops_mmap = {
248 .read = sysfs_kf_bin_read,
249 .write = sysfs_kf_bin_write,
Tejun Heof6acf8b2013-11-28 14:54:21 -0500250 .mmap = sysfs_kf_bin_mmap,
Daniel Vetter74b30192020-11-27 17:41:25 +0100251 .open = sysfs_kf_bin_open,
Tejun Heof6acf8b2013-11-28 14:54:21 -0500252};
253
Tejun Heo324a56e2013-12-11 14:11:53 -0500254int sysfs_add_file_mode_ns(struct kernfs_node *parent,
Christoph Hellwig5cf3bb02021-09-13 07:41:12 +0200255 const struct attribute *attr, umode_t mode, kuid_t uid,
256 kgid_t gid, const void *ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
Christoph Hellwig5cf3bb02021-09-13 07:41:12 +0200258 struct kobject *kobj = parent->priv;
259 const struct sysfs_ops *sysfs_ops = kobj->ktype->sysfs_ops;
Tejun Heo517e64f2013-11-28 14:54:29 -0500260 struct lock_class_key *key = NULL;
Christoph Hellwigd1a1a962021-09-13 07:41:13 +0200261 const struct kernfs_ops *ops = NULL;
Tejun Heo324a56e2013-12-11 14:11:53 -0500262 struct kernfs_node *kn;
Tejun Heoa26cd722007-06-14 03:45:14 +0900263
Christoph Hellwig5cf3bb02021-09-13 07:41:12 +0200264 /* 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 Heof6acf8b2013-11-28 14:54:21 -0500269
Christoph Hellwigd1a1a962021-09-13 07:41:13 +0200270 if (mode & SYSFS_PREALLOC) {
271 if (sysfs_ops->show && sysfs_ops->store)
Christoph Hellwig5cf3bb02021-09-13 07:41:12 +0200272 ops = &sysfs_prealloc_kfops_rw;
Christoph Hellwigd1a1a962021-09-13 07:41:13 +0200273 else if (sysfs_ops->show)
Christoph Hellwig5cf3bb02021-09-13 07:41:12 +0200274 ops = &sysfs_prealloc_kfops_ro;
Christoph Hellwigd1a1a962021-09-13 07:41:13 +0200275 else if (sysfs_ops->store)
Christoph Hellwig5cf3bb02021-09-13 07:41:12 +0200276 ops = &sysfs_prealloc_kfops_wo;
Christoph Hellwigd1a1a962021-09-13 07:41:13 +0200277 } 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 Hellwig5cf3bb02021-09-13 07:41:12 +0200283 ops = &sysfs_file_kfops_wo;
Christoph Hellwigd1a1a962021-09-13 07:41:13 +0200284 }
285
286 if (!ops)
Christoph Hellwig5cf3bb02021-09-13 07:41:12 +0200287 ops = &sysfs_file_kfops_empty;
Tejun Heof6acf8b2013-11-28 14:54:21 -0500288
Tejun Heo517e64f2013-11-28 14:54:29 -0500289#ifdef CONFIG_DEBUG_LOCK_ALLOC
290 if (!attr->ignore_lockdep)
291 key = attr->key ?: (struct lock_class_key *)&attr->skey;
292#endif
Dmitry Torokhov5f818802018-07-20 21:56:48 +0000293
294 kn = __kernfs_create_file(parent, attr->name, mode & 0777, uid, gid,
Christoph Hellwig5cf3bb02021-09-13 07:41:12 +0200295 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
304int 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 Heo324a56e2013-12-11 14:11:53 -0500331 if (IS_ERR(kn)) {
332 if (PTR_ERR(kn) == -EEXIST)
333 sysfs_warn_dup(parent, attr->name);
334 return PTR_ERR(kn);
Tejun Heo496f7392013-11-28 14:54:24 -0500335 }
336 return 0;
337}
338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339/**
Tejun Heo58292cbe2013-09-11 22:29:04 -0400340 * 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 Torvalds1da177e2005-04-16 15:20:36 -0700344 */
Tejun Heo58292cbe2013-09-11 22:29:04 -0400345int sysfs_create_file_ns(struct kobject *kobj, const struct attribute *attr,
346 const void *ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
Dmitry Torokhov5f818802018-07-20 21:56:48 +0000348 kuid_t uid;
349 kgid_t gid;
350
Greg Kroah-Hartmande96e9f2019-01-03 10:23:47 +0100351 if (WARN_ON(!kobj || !kobj->sd || !attr))
352 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Dmitry Torokhov5f818802018-07-20 21:56:48 +0000354 kobject_get_ownership(kobj, &uid, &gid);
Christoph Hellwig5cf3bb02021-09-13 07:41:12 +0200355 return sysfs_add_file_mode_ns(kobj->sd, attr, attr->mode, uid, gid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356}
Tejun Heo58292cbe2013-09-11 22:29:04 -0400357EXPORT_SYMBOL_GPL(sysfs_create_file_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Jani Nikula9ee46852018-10-04 17:37:49 +0300359int sysfs_create_files(struct kobject *kobj, const struct attribute * const *ptr)
Andi Kleen1c205ae2010-01-05 12:48:01 +0100360{
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-Hartman1b866752013-08-21 16:17:47 -0700371EXPORT_SYMBOL_GPL(sysfs_create_files);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373/**
Alan Sterndfa87c82007-02-20 15:02:44 -0500374 * 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 */
379int sysfs_add_file_to_group(struct kobject *kobj,
380 const struct attribute *attr, const char *group)
381{
Tejun Heo324a56e2013-12-11 14:11:53 -0500382 struct kernfs_node *parent;
Dmitry Torokhov5f818802018-07-20 21:56:48 +0000383 kuid_t uid;
384 kgid_t gid;
Alan Sterndfa87c82007-02-20 15:02:44 -0500385 int error;
386
Tejun Heoccf73cf2013-11-28 14:54:30 -0500387 if (group) {
Tejun Heo324a56e2013-12-11 14:11:53 -0500388 parent = kernfs_find_and_get(kobj->sd, group);
Tejun Heoccf73cf2013-11-28 14:54:30 -0500389 } else {
Tejun Heo324a56e2013-12-11 14:11:53 -0500390 parent = kobj->sd;
391 kernfs_get(parent);
Tejun Heoccf73cf2013-11-28 14:54:30 -0500392 }
James Bottomley11f24fb2008-01-02 18:44:05 -0600393
Tejun Heo324a56e2013-12-11 14:11:53 -0500394 if (!parent)
Tejun Heo608e2662007-06-14 04:27:22 +0900395 return -ENOENT;
396
Dmitry Torokhov5f818802018-07-20 21:56:48 +0000397 kobject_get_ownership(kobj, &uid, &gid);
Christoph Hellwig5cf3bb02021-09-13 07:41:12 +0200398 error = sysfs_add_file_mode_ns(parent, attr, attr->mode, uid, gid,
399 NULL);
Tejun Heo324a56e2013-12-11 14:11:53 -0500400 kernfs_put(parent);
Tejun Heo608e2662007-06-14 04:27:22 +0900401
Alan Sterndfa87c82007-02-20 15:02:44 -0500402 return error;
403}
404EXPORT_SYMBOL_GPL(sysfs_add_file_to_group);
405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406/**
Kay Sievers31e5abe2005-04-18 21:57:32 -0700407 * 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 Delvare49c19402010-07-02 16:54:05 +0200413int sysfs_chmod_file(struct kobject *kobj, const struct attribute *attr,
Al Viro48176a92011-07-24 03:40:40 -0400414 umode_t mode)
Kay Sievers31e5abe2005-04-18 21:57:32 -0700415{
Tejun Heo324a56e2013-12-11 14:11:53 -0500416 struct kernfs_node *kn;
Maneesh Sonibc062b12005-07-29 12:13:35 -0700417 struct iattr newattrs;
Tejun Heo51225032007-06-14 04:27:25 +0900418 int rc;
Kay Sievers31e5abe2005-04-18 21:57:32 -0700419
Tejun Heo324a56e2013-12-11 14:11:53 -0500420 kn = kernfs_find_and_get(kobj->sd, attr->name);
421 if (!kn)
Tejun Heo5d604182013-11-23 17:21:52 -0500422 return -ENOENT;
Tejun Heo51225032007-06-14 04:27:25 +0900423
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500424 newattrs.ia_mode = (mode & S_IALLUGO) | (kn->mode & ~S_IALLUGO);
Eric W. Biederman4c6974f2009-11-07 23:27:02 -0800425 newattrs.ia_valid = ATTR_MODE;
Tejun Heof88123e2007-09-20 16:05:10 +0900426
Tejun Heo324a56e2013-12-11 14:11:53 -0500427 rc = kernfs_setattr(kn, &newattrs);
Tejun Heo5d604182013-11-23 17:21:52 -0500428
Tejun Heo324a56e2013-12-11 14:11:53 -0500429 kernfs_put(kn);
Tejun Heo51225032007-06-14 04:27:25 +0900430 return rc;
Kay Sievers31e5abe2005-04-18 21:57:32 -0700431}
432EXPORT_SYMBOL_GPL(sysfs_chmod_file);
433
Kay Sievers31e5abe2005-04-18 21:57:32 -0700434/**
Bart Van Assche2afc9162018-08-02 10:51:40 -0700435 * 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 */
444struct 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}
455EXPORT_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 */
468void 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}
476EXPORT_SYMBOL_GPL(sysfs_unbreak_active_protection);
477
478/**
Tejun Heo58292cbe2013-09-11 22:29:04 -0400479 * 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 Torvalds1da177e2005-04-16 15:20:36 -0700483 *
Tejun Heo58292cbe2013-09-11 22:29:04 -0400484 * Hash the attribute name and namespace tag and kill the victim.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 */
Tejun Heo58292cbe2013-09-11 22:29:04 -0400486void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr,
487 const void *ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
Tejun Heo324a56e2013-12-11 14:11:53 -0500489 struct kernfs_node *parent = kobj->sd;
Eric W. Biederman487505c2011-10-12 21:53:38 +0000490
Tejun Heo324a56e2013-12-11 14:11:53 -0500491 kernfs_remove_by_name_ns(parent, attr->name, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492}
Tejun Heo58292cbe2013-09-11 22:29:04 -0400493EXPORT_SYMBOL_GPL(sysfs_remove_file_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Tejun Heo6b0afc22014-02-03 14:03:01 -0500495/**
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 */
502bool 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 Wang9ddacff2020-05-11 15:51:07 +0200517EXPORT_SYMBOL_GPL(sysfs_remove_file_self);
Tejun Heo6b0afc22014-02-03 14:03:01 -0500518
Jani Nikula9ee46852018-10-04 17:37:49 +0300519void sysfs_remove_files(struct kobject *kobj, const struct attribute * const *ptr)
Andi Kleen1c205ae2010-01-05 12:48:01 +0100520{
521 int i;
Stephen Martin4bd4e922018-12-20 13:50:28 -0800522
Andi Kleen1c205ae2010-01-05 12:48:01 +0100523 for (i = 0; ptr[i]; i++)
524 sysfs_remove_file(kobj, ptr[i]);
525}
Greg Kroah-Hartman1b866752013-08-21 16:17:47 -0700526EXPORT_SYMBOL_GPL(sysfs_remove_files);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Alan Sterndfa87c82007-02-20 15:02:44 -0500528/**
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 */
534void sysfs_remove_file_from_group(struct kobject *kobj,
535 const struct attribute *attr, const char *group)
536{
Tejun Heo324a56e2013-12-11 14:11:53 -0500537 struct kernfs_node *parent;
Alan Sterndfa87c82007-02-20 15:02:44 -0500538
Tejun Heoccf73cf2013-11-28 14:54:30 -0500539 if (group) {
Tejun Heo324a56e2013-12-11 14:11:53 -0500540 parent = kernfs_find_and_get(kobj->sd, group);
Tejun Heoccf73cf2013-11-28 14:54:30 -0500541 } else {
Tejun Heo324a56e2013-12-11 14:11:53 -0500542 parent = kobj->sd;
543 kernfs_get(parent);
Tejun Heoccf73cf2013-11-28 14:54:30 -0500544 }
545
Tejun Heo324a56e2013-12-11 14:11:53 -0500546 if (parent) {
547 kernfs_remove_by_name(parent, attr->name);
548 kernfs_put(parent);
Alan Sterndfa87c82007-02-20 15:02:44 -0500549 }
550}
551EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group);
552
Tejun Heo3124eb12013-10-01 17:42:09 -0400553/**
554 * sysfs_create_bin_file - create binary file for object.
555 * @kobj: object.
556 * @attr: attribute descriptor.
557 */
558int sysfs_create_bin_file(struct kobject *kobj,
559 const struct bin_attribute *attr)
560{
Dmitry Torokhov5f818802018-07-20 21:56:48 +0000561 kuid_t uid;
562 kgid_t gid;
563
Greg Kroah-Hartmande96e9f2019-01-03 10:23:47 +0100564 if (WARN_ON(!kobj || !kobj->sd || !attr))
565 return -EINVAL;
Tejun Heo3124eb12013-10-01 17:42:09 -0400566
Dmitry Torokhov5f818802018-07-20 21:56:48 +0000567 kobject_get_ownership(kobj, &uid, &gid);
Christoph Hellwig5cf3bb02021-09-13 07:41:12 +0200568 return sysfs_add_bin_file_mode_ns(kobj->sd, attr, attr->attr.mode, uid,
569 gid, NULL);
Tejun Heo3124eb12013-10-01 17:42:09 -0400570}
571EXPORT_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 */
578void sysfs_remove_bin_file(struct kobject *kobj,
579 const struct bin_attribute *attr)
580{
Tejun Heo879f40d2013-11-23 17:21:49 -0500581 kernfs_remove_by_name(kobj->sd, attr->attr.name);
Tejun Heo3124eb12013-10-01 17:42:09 -0400582}
583EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);
Christian Braunerf70ce182020-02-27 04:37:11 +0100584
585static 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 Brauner0666a3a2020-02-27 04:37:12 +0100597 * 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 */
610int 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
632out:
633 kernfs_put(kn);
634 return error;
635}
636
637/**
Christian Braunerf70ce182020-02-27 04:37:11 +0100638 * 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 */
649int 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}
671EXPORT_SYMBOL_GPL(sysfs_file_change_owner);
Christian Brauner2c4f9402020-02-27 04:37:14 +0100672
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 */
691int 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}
731EXPORT_SYMBOL_GPL(sysfs_change_owner);
Joe Perches2efc4592020-09-16 13:40:38 -0700732
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 */
742int 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}
757EXPORT_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 */
770int 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}
785EXPORT_SYMBOL_GPL(sysfs_emit_at);