blob: 9aefa7779b29af4197894f00fd4091eb1315725c [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
Tejun Heof5c16f22014-05-19 15:52:10 -040048 /* acquire buffer and ensure that it's >= PAGE_SIZE and clear */
Tejun Heo13c589d2013-10-01 17:42:02 -040049 count = seq_get_buf(sf, &buf);
50 if (count < PAGE_SIZE) {
51 seq_commit(sf, -1);
52 return 0;
53 }
Tejun Heof5c16f22014-05-19 15:52:10 -040054 memset(buf, 0, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Tejun Heo13c589d2013-10-01 17:42:02 -040056 /*
Tejun Heoc2b19da2013-11-28 14:54:16 -050057 * Invoke show(). Control may reach here via seq file lseek even
58 * if @ops->show() isn't implemented.
Tejun Heo13c589d2013-10-01 17:42:02 -040059 */
Tejun Heoc2b19da2013-11-28 14:54:16 -050060 if (ops->show) {
Tejun Heo324a56e2013-12-11 14:11:53 -050061 count = ops->show(kobj, of->kn->priv, buf);
Tejun Heoc2b19da2013-11-28 14:54:16 -050062 if (count < 0)
63 return count;
64 }
Tejun Heo0ab66082007-06-14 03:45:16 +090065
Miao Xie8118a852007-11-21 14:55:19 -080066 /*
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 Morton815d2d52008-03-04 15:09:07 -080070 if (count >= (ssize_t)PAGE_SIZE) {
Sergey Senozhatsky9e6d35f2017-12-11 21:50:22 +090071 printk("fill_read_buffer: %pS returned bad count\n",
72 ops->show);
Andrew Morton815d2d52008-03-04 15:09:07 -080073 /* Try to struggle along */
74 count = PAGE_SIZE - 1;
75 }
Tejun Heo13c589d2013-10-01 17:42:02 -040076 seq_commit(sf, count);
77 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
79
Tejun Heoc525aad2013-12-11 14:11:55 -050080static ssize_t sysfs_kf_bin_read(struct kernfs_open_file *of, char *buf,
Tejun Heoc2b19da2013-11-28 14:54:16 -050081 size_t count, loff_t pos)
Tejun Heo2f0c6b72013-10-01 17:42:06 -040082{
Tejun Heo324a56e2013-12-11 14:11:53 -050083 struct bin_attribute *battr = of->kn->priv;
Tejun Heoadc5e8b2013-12-11 14:11:54 -050084 struct kobject *kobj = of->kn->parent->priv;
Tejun Heoc2b19da2013-11-28 14:54:16 -050085 loff_t size = file_inode(of->file)->i_size;
Tejun Heo2f0c6b72013-10-01 17:42:06 -040086
Tejun Heoc2b19da2013-11-28 14:54:16 -050087 if (!count)
Tejun Heo2f0c6b72013-10-01 17:42:06 -040088 return 0;
89
90 if (size) {
Vladimir Zapolskiyeaa5cd92015-05-22 00:21:16 +030091 if (pos >= size)
Tejun Heo2f0c6b72013-10-01 17:42:06 -040092 return 0;
Tejun Heoc2b19da2013-11-28 14:54:16 -050093 if (pos + count > size)
94 count = size - pos;
Tejun Heo2f0c6b72013-10-01 17:42:06 -040095 }
96
Tejun Heoc2b19da2013-11-28 14:54:16 -050097 if (!battr->read)
98 return -EIO;
99
100 return battr->read(of->file, kobj, battr, buf, pos, count);
101}
102
NeilBrown4ef67a82014-10-14 16:57:26 +1100103/* kernfs read callback for regular sysfs files with pre-alloc */
104static 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;
NeilBrownc8a139d2017-04-03 11:30:34 +1000109 ssize_t len;
NeilBrown4ef67a82014-10-14 16:57:26 +1100110
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 Khlebnikov17d07742016-06-22 21:42:16 +0300115 if (WARN_ON_ONCE(buf != of->prealloc_buf))
NeilBrown4ef67a82014-10-14 16:57:26 +1100116 return 0;
NeilBrown65da3482015-08-06 08:27:55 +1000117 len = ops->show(kobj, of->kn->priv, buf);
NeilBrownc8a139d2017-04-03 11:30:34 +1000118 if (len < 0)
119 return len;
Konstantin Khlebnikov17d07742016-06-22 21:42:16 +0300120 if (pos) {
121 if (len <= pos)
122 return 0;
123 len -= pos;
124 memmove(buf, buf + pos, len);
125 }
NeilBrownc8a139d2017-04-03 11:30:34 +1000126 return min_t(ssize_t, count, len);
NeilBrown4ef67a82014-10-14 16:57:26 +1100127}
128
Tejun Heo50b38ca2013-11-28 14:54:17 -0500129/* kernfs write callback for regular sysfs files */
Tejun Heoc525aad2013-12-11 14:11:55 -0500130static ssize_t sysfs_kf_write(struct kernfs_open_file *of, char *buf,
Tejun Heo50b38ca2013-11-28 14:54:17 -0500131 size_t count, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
Tejun Heo324a56e2013-12-11 14:11:53 -0500133 const struct sysfs_ops *ops = sysfs_file_ops(of->kn);
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500134 struct kobject *kobj = of->kn->parent->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Tejun Heo50b38ca2013-11-28 14:54:17 -0500136 if (!count)
137 return 0;
138
Tejun Heo324a56e2013-12-11 14:11:53 -0500139 return ops->store(kobj, of->kn->priv, buf, count);
Tejun Heo50b38ca2013-11-28 14:54:17 -0500140}
141
142/* kernfs write callback for bin sysfs files */
Tejun Heoc525aad2013-12-11 14:11:55 -0500143static ssize_t sysfs_kf_bin_write(struct kernfs_open_file *of, char *buf,
Tejun Heo50b38ca2013-11-28 14:54:17 -0500144 size_t count, loff_t pos)
145{
Tejun Heo324a56e2013-12-11 14:11:53 -0500146 struct bin_attribute *battr = of->kn->priv;
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500147 struct kobject *kobj = of->kn->parent->priv;
Tejun Heo50b38ca2013-11-28 14:54:17 -0500148 loff_t size = file_inode(of->file)->i_size;
149
150 if (size) {
151 if (size <= pos)
Vladimir Zapolskiy09368962014-09-24 18:21:04 +0300152 return -EFBIG;
Tejun Heo50b38ca2013-11-28 14:54:17 -0500153 count = min_t(ssize_t, count, size - pos);
Tejun Heo8ef445f2013-10-01 17:42:01 -0400154 }
Tejun Heo50b38ca2013-11-28 14:54:17 -0500155 if (!count)
156 return 0;
Tejun Heo0ab66082007-06-14 03:45:16 +0900157
Tejun Heo50b38ca2013-11-28 14:54:17 -0500158 if (!battr->write)
159 return -EIO;
Tejun Heof9b9a622013-10-01 17:42:05 -0400160
Tejun Heo50b38ca2013-11-28 14:54:17 -0500161 return battr->write(of->file, kobj, battr, buf, pos, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
163
Tejun Heoc525aad2013-12-11 14:11:55 -0500164static int sysfs_kf_bin_mmap(struct kernfs_open_file *of,
Tejun Heofdbffaa2013-11-28 14:54:18 -0500165 struct vm_area_struct *vma)
166{
Tejun Heo324a56e2013-12-11 14:11:53 -0500167 struct bin_attribute *battr = of->kn->priv;
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500168 struct kobject *kobj = of->kn->parent->priv;
Tejun Heofdbffaa2013-11-28 14:54:18 -0500169
Tejun Heofdbffaa2013-11-28 14:54:18 -0500170 return battr->mmap(of->file, kobj, battr, vma);
171}
172
Daniel Vetter74b30192020-11-27 17:41:25 +0100173static 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 Heo324a56e2013-12-11 14:11:53 -0500183void sysfs_notify(struct kobject *kobj, const char *dir, const char *attr)
NeilBrown4508a7a2006-03-20 17:53:53 +1100184{
Tejun Heo324a56e2013-12-11 14:11:53 -0500185 struct kernfs_node *kn = kobj->sd, *tmp;
NeilBrown4508a7a2006-03-20 17:53:53 +1100186
Tejun Heo324a56e2013-12-11 14:11:53 -0500187 if (kn && dir)
188 kn = kernfs_find_and_get(kn, dir);
Tejun Heo024f6472013-11-28 14:54:27 -0500189 else
Tejun Heo324a56e2013-12-11 14:11:53 -0500190 kernfs_get(kn);
Tejun Heo51225032007-06-14 04:27:25 +0900191
Tejun Heo324a56e2013-12-11 14:11:53 -0500192 if (kn && attr) {
193 tmp = kernfs_find_and_get(kn, attr);
194 kernfs_put(kn);
195 kn = tmp;
Tejun Heo024f6472013-11-28 14:54:27 -0500196 }
197
Tejun Heo324a56e2013-12-11 14:11:53 -0500198 if (kn) {
199 kernfs_notify(kn);
200 kernfs_put(kn);
Tejun Heo024f6472013-11-28 14:54:27 -0500201 }
NeilBrown4508a7a2006-03-20 17:53:53 +1100202}
203EXPORT_SYMBOL_GPL(sysfs_notify);
204
Tejun Heof6acf8b2013-11-28 14:54:21 -0500205static const struct kernfs_ops sysfs_file_kfops_empty = {
206};
207
208static const struct kernfs_ops sysfs_file_kfops_ro = {
209 .seq_show = sysfs_kf_seq_show,
210};
211
212static const struct kernfs_ops sysfs_file_kfops_wo = {
213 .write = sysfs_kf_write,
214};
215
216static const struct kernfs_ops sysfs_file_kfops_rw = {
217 .seq_show = sysfs_kf_seq_show,
218 .write = sysfs_kf_write,
219};
220
NeilBrown4ef67a82014-10-14 16:57:26 +1100221static const struct kernfs_ops sysfs_prealloc_kfops_ro = {
222 .read = sysfs_kf_read,
223 .prealloc = true,
224};
225
NeilBrown2b758692014-10-13 16:41:28 +1100226static const struct kernfs_ops sysfs_prealloc_kfops_wo = {
227 .write = sysfs_kf_write,
228 .prealloc = true,
229};
230
231static const struct kernfs_ops sysfs_prealloc_kfops_rw = {
NeilBrown4ef67a82014-10-14 16:57:26 +1100232 .read = sysfs_kf_read,
NeilBrown2b758692014-10-13 16:41:28 +1100233 .write = sysfs_kf_write,
234 .prealloc = true,
235};
236
Tejun Heof6acf8b2013-11-28 14:54:21 -0500237static const struct kernfs_ops sysfs_bin_kfops_ro = {
238 .read = sysfs_kf_bin_read,
239};
240
241static const struct kernfs_ops sysfs_bin_kfops_wo = {
242 .write = sysfs_kf_bin_write,
243};
244
245static const struct kernfs_ops sysfs_bin_kfops_rw = {
246 .read = sysfs_kf_bin_read,
247 .write = sysfs_kf_bin_write,
Tejun Heo9b2db6e2013-12-10 09:29:17 -0500248};
249
250static const struct kernfs_ops sysfs_bin_kfops_mmap = {
251 .read = sysfs_kf_bin_read,
252 .write = sysfs_kf_bin_write,
Tejun Heof6acf8b2013-11-28 14:54:21 -0500253 .mmap = sysfs_kf_bin_mmap,
Daniel Vetter74b30192020-11-27 17:41:25 +0100254 .open = sysfs_kf_bin_open,
Tejun Heof6acf8b2013-11-28 14:54:21 -0500255};
256
Tejun Heo324a56e2013-12-11 14:11:53 -0500257int sysfs_add_file_mode_ns(struct kernfs_node *parent,
Tejun Heoa7dc66d2013-11-28 14:54:23 -0500258 const struct attribute *attr, bool is_bin,
Dmitry Torokhov5f818802018-07-20 21:56:48 +0000259 umode_t mode, kuid_t uid, kgid_t gid, const void *ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
Tejun Heo517e64f2013-11-28 14:54:29 -0500261 struct lock_class_key *key = NULL;
Tejun Heof6acf8b2013-11-28 14:54:21 -0500262 const struct kernfs_ops *ops;
Tejun Heo324a56e2013-12-11 14:11:53 -0500263 struct kernfs_node *kn;
Tejun Heo471bd7b2013-11-28 14:54:22 -0500264 loff_t size;
Tejun Heoa26cd722007-06-14 03:45:14 +0900265
Tejun Heoa7dc66d2013-11-28 14:54:23 -0500266 if (!is_bin) {
Tejun Heo324a56e2013-12-11 14:11:53 -0500267 struct kobject *kobj = parent->priv;
Tejun Heof6acf8b2013-11-28 14:54:21 -0500268 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
NeilBrown2b758692014-10-13 16:41:28 +1100276 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;
NeilBrown4ef67a82014-10-14 16:57:26 +1100281 } 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) {
NeilBrown2b758692014-10-13 16:41:28 +1100287 if (mode & SYSFS_PREALLOC)
288 ops = &sysfs_prealloc_kfops_wo;
289 else
290 ops = &sysfs_file_kfops_wo;
291 } else
Tejun Heof6acf8b2013-11-28 14:54:21 -0500292 ops = &sysfs_file_kfops_empty;
Tejun Heo471bd7b2013-11-28 14:54:22 -0500293
294 size = PAGE_SIZE;
Tejun Heof6acf8b2013-11-28 14:54:21 -0500295 } else {
296 struct bin_attribute *battr = (void *)attr;
297
Tejun Heo9b2db6e2013-12-10 09:29:17 -0500298 if (battr->mmap)
299 ops = &sysfs_bin_kfops_mmap;
300 else if (battr->read && battr->write)
Tejun Heof6acf8b2013-11-28 14:54:21 -0500301 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 Heo471bd7b2013-11-28 14:54:22 -0500308
309 size = battr->size;
Tejun Heof6acf8b2013-11-28 14:54:21 -0500310 }
311
Tejun Heo517e64f2013-11-28 14:54:29 -0500312#ifdef CONFIG_DEBUG_LOCK_ALLOC
313 if (!attr->ignore_lockdep)
314 key = attr->key ?: (struct lock_class_key *)&attr->skey;
315#endif
Dmitry Torokhov5f818802018-07-20 21:56:48 +0000316
317 kn = __kernfs_create_file(parent, attr->name, mode & 0777, uid, gid,
Dmitry Torokhov488dee92018-07-20 21:56:47 +0000318 size, ops, (void *)attr, ns, key);
Tejun Heo324a56e2013-12-11 14:11:53 -0500319 if (IS_ERR(kn)) {
320 if (PTR_ERR(kn) == -EEXIST)
321 sysfs_warn_dup(parent, attr->name);
322 return PTR_ERR(kn);
Tejun Heo496f7392013-11-28 14:54:24 -0500323 }
324 return 0;
325}
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327/**
Tejun Heo58292cbe2013-09-11 22:29:04 -0400328 * 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 Torvalds1da177e2005-04-16 15:20:36 -0700332 */
Tejun Heo58292cbe2013-09-11 22:29:04 -0400333int sysfs_create_file_ns(struct kobject *kobj, const struct attribute *attr,
334 const void *ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
Dmitry Torokhov5f818802018-07-20 21:56:48 +0000336 kuid_t uid;
337 kgid_t gid;
338
Greg Kroah-Hartmande96e9f2019-01-03 10:23:47 +0100339 if (WARN_ON(!kobj || !kobj->sd || !attr))
340 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Dmitry Torokhov5f818802018-07-20 21:56:48 +0000342 kobject_get_ownership(kobj, &uid, &gid);
343 return sysfs_add_file_mode_ns(kobj->sd, attr, false, attr->mode,
344 uid, gid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346}
Tejun Heo58292cbe2013-09-11 22:29:04 -0400347EXPORT_SYMBOL_GPL(sysfs_create_file_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Jani Nikula9ee46852018-10-04 17:37:49 +0300349int sysfs_create_files(struct kobject *kobj, const struct attribute * const *ptr)
Andi Kleen1c205ae2010-01-05 12:48:01 +0100350{
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-Hartman1b866752013-08-21 16:17:47 -0700361EXPORT_SYMBOL_GPL(sysfs_create_files);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363/**
Alan Sterndfa87c82007-02-20 15:02:44 -0500364 * 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 */
369int sysfs_add_file_to_group(struct kobject *kobj,
370 const struct attribute *attr, const char *group)
371{
Tejun Heo324a56e2013-12-11 14:11:53 -0500372 struct kernfs_node *parent;
Dmitry Torokhov5f818802018-07-20 21:56:48 +0000373 kuid_t uid;
374 kgid_t gid;
Alan Sterndfa87c82007-02-20 15:02:44 -0500375 int error;
376
Tejun Heoccf73cf2013-11-28 14:54:30 -0500377 if (group) {
Tejun Heo324a56e2013-12-11 14:11:53 -0500378 parent = kernfs_find_and_get(kobj->sd, group);
Tejun Heoccf73cf2013-11-28 14:54:30 -0500379 } else {
Tejun Heo324a56e2013-12-11 14:11:53 -0500380 parent = kobj->sd;
381 kernfs_get(parent);
Tejun Heoccf73cf2013-11-28 14:54:30 -0500382 }
James Bottomley11f24fb2008-01-02 18:44:05 -0600383
Tejun Heo324a56e2013-12-11 14:11:53 -0500384 if (!parent)
Tejun Heo608e2662007-06-14 04:27:22 +0900385 return -ENOENT;
386
Dmitry Torokhov5f818802018-07-20 21:56:48 +0000387 kobject_get_ownership(kobj, &uid, &gid);
Tyler Hicksd1753392018-07-27 21:33:27 +0000388 error = sysfs_add_file_mode_ns(parent, attr, false,
Dmitry Torokhov5f818802018-07-20 21:56:48 +0000389 attr->mode, uid, gid, NULL);
Tejun Heo324a56e2013-12-11 14:11:53 -0500390 kernfs_put(parent);
Tejun Heo608e2662007-06-14 04:27:22 +0900391
Alan Sterndfa87c82007-02-20 15:02:44 -0500392 return error;
393}
394EXPORT_SYMBOL_GPL(sysfs_add_file_to_group);
395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396/**
Kay Sievers31e5abe2005-04-18 21:57:32 -0700397 * 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 Delvare49c19402010-07-02 16:54:05 +0200403int sysfs_chmod_file(struct kobject *kobj, const struct attribute *attr,
Al Viro48176a92011-07-24 03:40:40 -0400404 umode_t mode)
Kay Sievers31e5abe2005-04-18 21:57:32 -0700405{
Tejun Heo324a56e2013-12-11 14:11:53 -0500406 struct kernfs_node *kn;
Maneesh Sonibc062b12005-07-29 12:13:35 -0700407 struct iattr newattrs;
Tejun Heo51225032007-06-14 04:27:25 +0900408 int rc;
Kay Sievers31e5abe2005-04-18 21:57:32 -0700409
Tejun Heo324a56e2013-12-11 14:11:53 -0500410 kn = kernfs_find_and_get(kobj->sd, attr->name);
411 if (!kn)
Tejun Heo5d604182013-11-23 17:21:52 -0500412 return -ENOENT;
Tejun Heo51225032007-06-14 04:27:25 +0900413
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500414 newattrs.ia_mode = (mode & S_IALLUGO) | (kn->mode & ~S_IALLUGO);
Eric W. Biederman4c6974f2009-11-07 23:27:02 -0800415 newattrs.ia_valid = ATTR_MODE;
Tejun Heof88123e2007-09-20 16:05:10 +0900416
Tejun Heo324a56e2013-12-11 14:11:53 -0500417 rc = kernfs_setattr(kn, &newattrs);
Tejun Heo5d604182013-11-23 17:21:52 -0500418
Tejun Heo324a56e2013-12-11 14:11:53 -0500419 kernfs_put(kn);
Tejun Heo51225032007-06-14 04:27:25 +0900420 return rc;
Kay Sievers31e5abe2005-04-18 21:57:32 -0700421}
422EXPORT_SYMBOL_GPL(sysfs_chmod_file);
423
Kay Sievers31e5abe2005-04-18 21:57:32 -0700424/**
Bart Van Assche2afc9162018-08-02 10:51:40 -0700425 * 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 */
434struct 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}
445EXPORT_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 */
458void 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}
466EXPORT_SYMBOL_GPL(sysfs_unbreak_active_protection);
467
468/**
Tejun Heo58292cbe2013-09-11 22:29:04 -0400469 * 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 Torvalds1da177e2005-04-16 15:20:36 -0700473 *
Tejun Heo58292cbe2013-09-11 22:29:04 -0400474 * Hash the attribute name and namespace tag and kill the victim.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 */
Tejun Heo58292cbe2013-09-11 22:29:04 -0400476void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr,
477 const void *ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
Tejun Heo324a56e2013-12-11 14:11:53 -0500479 struct kernfs_node *parent = kobj->sd;
Eric W. Biederman487505c2011-10-12 21:53:38 +0000480
Tejun Heo324a56e2013-12-11 14:11:53 -0500481 kernfs_remove_by_name_ns(parent, attr->name, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482}
Tejun Heo58292cbe2013-09-11 22:29:04 -0400483EXPORT_SYMBOL_GPL(sysfs_remove_file_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Tejun Heo6b0afc22014-02-03 14:03:01 -0500485/**
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 */
492bool 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 Wang9ddacff2020-05-11 15:51:07 +0200507EXPORT_SYMBOL_GPL(sysfs_remove_file_self);
Tejun Heo6b0afc22014-02-03 14:03:01 -0500508
Jani Nikula9ee46852018-10-04 17:37:49 +0300509void sysfs_remove_files(struct kobject *kobj, const struct attribute * const *ptr)
Andi Kleen1c205ae2010-01-05 12:48:01 +0100510{
511 int i;
Stephen Martin4bd4e922018-12-20 13:50:28 -0800512
Andi Kleen1c205ae2010-01-05 12:48:01 +0100513 for (i = 0; ptr[i]; i++)
514 sysfs_remove_file(kobj, ptr[i]);
515}
Greg Kroah-Hartman1b866752013-08-21 16:17:47 -0700516EXPORT_SYMBOL_GPL(sysfs_remove_files);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Alan Sterndfa87c82007-02-20 15:02:44 -0500518/**
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 */
524void sysfs_remove_file_from_group(struct kobject *kobj,
525 const struct attribute *attr, const char *group)
526{
Tejun Heo324a56e2013-12-11 14:11:53 -0500527 struct kernfs_node *parent;
Alan Sterndfa87c82007-02-20 15:02:44 -0500528
Tejun Heoccf73cf2013-11-28 14:54:30 -0500529 if (group) {
Tejun Heo324a56e2013-12-11 14:11:53 -0500530 parent = kernfs_find_and_get(kobj->sd, group);
Tejun Heoccf73cf2013-11-28 14:54:30 -0500531 } else {
Tejun Heo324a56e2013-12-11 14:11:53 -0500532 parent = kobj->sd;
533 kernfs_get(parent);
Tejun Heoccf73cf2013-11-28 14:54:30 -0500534 }
535
Tejun Heo324a56e2013-12-11 14:11:53 -0500536 if (parent) {
537 kernfs_remove_by_name(parent, attr->name);
538 kernfs_put(parent);
Alan Sterndfa87c82007-02-20 15:02:44 -0500539 }
540}
541EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group);
542
Tejun Heo3124eb12013-10-01 17:42:09 -0400543/**
544 * sysfs_create_bin_file - create binary file for object.
545 * @kobj: object.
546 * @attr: attribute descriptor.
547 */
548int sysfs_create_bin_file(struct kobject *kobj,
549 const struct bin_attribute *attr)
550{
Dmitry Torokhov5f818802018-07-20 21:56:48 +0000551 kuid_t uid;
552 kgid_t gid;
553
Greg Kroah-Hartmande96e9f2019-01-03 10:23:47 +0100554 if (WARN_ON(!kobj || !kobj->sd || !attr))
555 return -EINVAL;
Tejun Heo3124eb12013-10-01 17:42:09 -0400556
Dmitry Torokhov5f818802018-07-20 21:56:48 +0000557 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 Heo3124eb12013-10-01 17:42:09 -0400560}
561EXPORT_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 */
568void sysfs_remove_bin_file(struct kobject *kobj,
569 const struct bin_attribute *attr)
570{
Tejun Heo879f40d2013-11-23 17:21:49 -0500571 kernfs_remove_by_name(kobj->sd, attr->attr.name);
Tejun Heo3124eb12013-10-01 17:42:09 -0400572}
573EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);
Christian Braunerf70ce182020-02-27 04:37:11 +0100574
575static 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 Brauner0666a3a2020-02-27 04:37:12 +0100587 * 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 */
600int 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
622out:
623 kernfs_put(kn);
624 return error;
625}
626
627/**
Christian Braunerf70ce182020-02-27 04:37:11 +0100628 * 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 */
639int 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}
661EXPORT_SYMBOL_GPL(sysfs_file_change_owner);
Christian Brauner2c4f9402020-02-27 04:37:14 +0100662
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 */
681int 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}
721EXPORT_SYMBOL_GPL(sysfs_change_owner);
Joe Perches2efc4592020-09-16 13:40:38 -0700722
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 */
732int 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}
747EXPORT_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 */
760int 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}
775EXPORT_SYMBOL_GPL(sysfs_emit_at);