blob: 1b8b91b67fdb7a4b192b9568ad1d85defdcd863a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Tejun Heo6d66f5c2007-09-20 17:31:38 +09002 * fs/sysfs/file.c - sysfs regular (text) file implementation
3 *
4 * Copyright (c) 2001-3 Patrick Mochel
5 * Copyright (c) 2007 SUSE Linux Products GmbH
6 * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
7 *
8 * This file is released under the GPLv2.
9 *
10 * Please see Documentation/filesystems/sysfs.txt for more information.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
13#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/kobject.h>
Andrew Morton815d2d52008-03-04 15:09:07 -080015#include <linux/kallsyms.h>
Robert P. J. Dayc6f87732008-03-13 22:41:52 -040016#include <linux/slab.h>
Oliver Neukum94bebf42006-12-20 10:52:44 +010017#include <linux/list.h>
Dave Young52e8c202007-07-26 11:03:54 +000018#include <linux/mutex.h>
Tejun Heo13c589d2013-10-01 17:42:02 -040019#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21#include "sysfs.h"
Tejun Heo414985a2013-11-28 14:54:34 -050022#include "../kernfs/kernfs-internal.h"
Tejun Heof6acf8b2013-11-28 14:54:21 -050023
24/*
Tejun Heo324a56e2013-12-11 14:11:53 -050025 * Determine ktype->sysfs_ops for the given kernfs_node. This function
Tejun Heo375b6112013-10-01 17:41:57 -040026 * must be called while holding an active reference.
27 */
Tejun Heo324a56e2013-12-11 14:11:53 -050028static const struct sysfs_ops *sysfs_file_ops(struct kernfs_node *kn)
Tejun Heo375b6112013-10-01 17:41:57 -040029{
Tejun Heoadc5e8b2013-12-11 14:11:54 -050030 struct kobject *kobj = kn->parent->priv;
Tejun Heo375b6112013-10-01 17:41:57 -040031
Tejun Heodf23fc32013-12-11 14:11:56 -050032 if (kn->flags & KERNFS_LOCKDEP)
Tejun Heo324a56e2013-12-11 14:11:53 -050033 lockdep_assert_held(kn);
Tejun Heo375b6112013-10-01 17:41:57 -040034 return kobj->ktype ? kobj->ktype->sysfs_ops : NULL;
35}
36
Tejun Heo13c589d2013-10-01 17:42:02 -040037/*
38 * Reads on sysfs are handled through seq_file, which takes care of hairy
39 * details like buffering and seeking. The following function pipes
40 * sysfs_ops->show() result through seq_file.
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 */
Tejun Heoc2b19da2013-11-28 14:54:16 -050042static int sysfs_kf_seq_show(struct seq_file *sf, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
Tejun Heoc525aad2013-12-11 14:11:55 -050044 struct kernfs_open_file *of = sf->private;
Tejun Heoadc5e8b2013-12-11 14:11:54 -050045 struct kobject *kobj = of->kn->parent->priv;
Tejun Heo324a56e2013-12-11 14:11:53 -050046 const struct sysfs_ops *ops = sysfs_file_ops(of->kn);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 ssize_t count;
Tejun Heoc2b19da2013-11-28 14:54:16 -050048 char *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Tejun Heo13c589d2013-10-01 17:42:02 -040050 /* acquire buffer and ensure that it's >= PAGE_SIZE */
51 count = seq_get_buf(sf, &buf);
52 if (count < PAGE_SIZE) {
53 seq_commit(sf, -1);
54 return 0;
55 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Tejun Heo13c589d2013-10-01 17:42:02 -040057 /*
Tejun Heoc2b19da2013-11-28 14:54:16 -050058 * Invoke show(). Control may reach here via seq file lseek even
59 * if @ops->show() isn't implemented.
Tejun Heo13c589d2013-10-01 17:42:02 -040060 */
Tejun Heoc2b19da2013-11-28 14:54:16 -050061 if (ops->show) {
Tejun Heo324a56e2013-12-11 14:11:53 -050062 count = ops->show(kobj, of->kn->priv, buf);
Tejun Heoc2b19da2013-11-28 14:54:16 -050063 if (count < 0)
64 return count;
65 }
Tejun Heo0ab66082007-06-14 03:45:16 +090066
Miao Xie8118a852007-11-21 14:55:19 -080067 /*
68 * The code works fine with PAGE_SIZE return but it's likely to
69 * indicate truncated result or overflow in normal use cases.
70 */
Andrew Morton815d2d52008-03-04 15:09:07 -080071 if (count >= (ssize_t)PAGE_SIZE) {
72 print_symbol("fill_read_buffer: %s returned bad count\n",
73 (unsigned long)ops->show);
74 /* Try to struggle along */
75 count = PAGE_SIZE - 1;
76 }
Tejun Heo13c589d2013-10-01 17:42:02 -040077 seq_commit(sf, count);
78 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079}
80
Tejun Heoc525aad2013-12-11 14:11:55 -050081static ssize_t sysfs_kf_bin_read(struct kernfs_open_file *of, char *buf,
Tejun Heoc2b19da2013-11-28 14:54:16 -050082 size_t count, loff_t pos)
Tejun Heo2f0c6b72013-10-01 17:42:06 -040083{
Tejun Heo324a56e2013-12-11 14:11:53 -050084 struct bin_attribute *battr = of->kn->priv;
Tejun Heoadc5e8b2013-12-11 14:11:54 -050085 struct kobject *kobj = of->kn->parent->priv;
Tejun Heoc2b19da2013-11-28 14:54:16 -050086 loff_t size = file_inode(of->file)->i_size;
Tejun Heo2f0c6b72013-10-01 17:42:06 -040087
Tejun Heoc2b19da2013-11-28 14:54:16 -050088 if (!count)
Tejun Heo2f0c6b72013-10-01 17:42:06 -040089 return 0;
90
91 if (size) {
Tejun Heoc2b19da2013-11-28 14:54:16 -050092 if (pos > size)
Tejun Heo2f0c6b72013-10-01 17:42:06 -040093 return 0;
Tejun Heoc2b19da2013-11-28 14:54:16 -050094 if (pos + count > size)
95 count = size - pos;
Tejun Heo2f0c6b72013-10-01 17:42:06 -040096 }
97
Tejun Heoc2b19da2013-11-28 14:54:16 -050098 if (!battr->read)
99 return -EIO;
100
101 return battr->read(of->file, kobj, battr, buf, pos, count);
102}
103
Tejun Heo50b38ca2013-11-28 14:54:17 -0500104/* kernfs write callback for regular sysfs files */
Tejun Heoc525aad2013-12-11 14:11:55 -0500105static ssize_t sysfs_kf_write(struct kernfs_open_file *of, char *buf,
Tejun Heo50b38ca2013-11-28 14:54:17 -0500106 size_t count, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
Tejun Heo324a56e2013-12-11 14:11:53 -0500108 const struct sysfs_ops *ops = sysfs_file_ops(of->kn);
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500109 struct kobject *kobj = of->kn->parent->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Tejun Heo50b38ca2013-11-28 14:54:17 -0500111 if (!count)
112 return 0;
113
Tejun Heo324a56e2013-12-11 14:11:53 -0500114 return ops->store(kobj, of->kn->priv, buf, count);
Tejun Heo50b38ca2013-11-28 14:54:17 -0500115}
116
117/* kernfs write callback for bin sysfs files */
Tejun Heoc525aad2013-12-11 14:11:55 -0500118static ssize_t sysfs_kf_bin_write(struct kernfs_open_file *of, char *buf,
Tejun Heo50b38ca2013-11-28 14:54:17 -0500119 size_t count, loff_t pos)
120{
Tejun Heo324a56e2013-12-11 14:11:53 -0500121 struct bin_attribute *battr = of->kn->priv;
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500122 struct kobject *kobj = of->kn->parent->priv;
Tejun Heo50b38ca2013-11-28 14:54:17 -0500123 loff_t size = file_inode(of->file)->i_size;
124
125 if (size) {
126 if (size <= pos)
127 return 0;
128 count = min_t(ssize_t, count, size - pos);
Tejun Heo8ef445f2013-10-01 17:42:01 -0400129 }
Tejun Heo50b38ca2013-11-28 14:54:17 -0500130 if (!count)
131 return 0;
Tejun Heo0ab66082007-06-14 03:45:16 +0900132
Tejun Heo50b38ca2013-11-28 14:54:17 -0500133 if (!battr->write)
134 return -EIO;
Tejun Heof9b9a622013-10-01 17:42:05 -0400135
Tejun Heo50b38ca2013-11-28 14:54:17 -0500136 return battr->write(of->file, kobj, battr, buf, pos, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
Tejun Heoc525aad2013-12-11 14:11:55 -0500139static int sysfs_kf_bin_mmap(struct kernfs_open_file *of,
Tejun Heofdbffaa2013-11-28 14:54:18 -0500140 struct vm_area_struct *vma)
141{
Tejun Heo324a56e2013-12-11 14:11:53 -0500142 struct bin_attribute *battr = of->kn->priv;
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500143 struct kobject *kobj = of->kn->parent->priv;
Tejun Heofdbffaa2013-11-28 14:54:18 -0500144
Tejun Heofdbffaa2013-11-28 14:54:18 -0500145 return battr->mmap(of->file, kobj, battr, vma);
146}
147
Tejun Heo324a56e2013-12-11 14:11:53 -0500148void sysfs_notify(struct kobject *kobj, const char *dir, const char *attr)
NeilBrown4508a7a2006-03-20 17:53:53 +1100149{
Tejun Heo324a56e2013-12-11 14:11:53 -0500150 struct kernfs_node *kn = kobj->sd, *tmp;
NeilBrown4508a7a2006-03-20 17:53:53 +1100151
Tejun Heo324a56e2013-12-11 14:11:53 -0500152 if (kn && dir)
153 kn = kernfs_find_and_get(kn, dir);
Tejun Heo024f6472013-11-28 14:54:27 -0500154 else
Tejun Heo324a56e2013-12-11 14:11:53 -0500155 kernfs_get(kn);
Tejun Heo51225032007-06-14 04:27:25 +0900156
Tejun Heo324a56e2013-12-11 14:11:53 -0500157 if (kn && attr) {
158 tmp = kernfs_find_and_get(kn, attr);
159 kernfs_put(kn);
160 kn = tmp;
Tejun Heo024f6472013-11-28 14:54:27 -0500161 }
162
Tejun Heo324a56e2013-12-11 14:11:53 -0500163 if (kn) {
164 kernfs_notify(kn);
165 kernfs_put(kn);
Tejun Heo024f6472013-11-28 14:54:27 -0500166 }
NeilBrown4508a7a2006-03-20 17:53:53 +1100167}
168EXPORT_SYMBOL_GPL(sysfs_notify);
169
Tejun Heof6acf8b2013-11-28 14:54:21 -0500170static const struct kernfs_ops sysfs_file_kfops_empty = {
171};
172
173static const struct kernfs_ops sysfs_file_kfops_ro = {
174 .seq_show = sysfs_kf_seq_show,
175};
176
177static const struct kernfs_ops sysfs_file_kfops_wo = {
178 .write = sysfs_kf_write,
179};
180
181static const struct kernfs_ops sysfs_file_kfops_rw = {
182 .seq_show = sysfs_kf_seq_show,
183 .write = sysfs_kf_write,
184};
185
186static const struct kernfs_ops sysfs_bin_kfops_ro = {
187 .read = sysfs_kf_bin_read,
188};
189
190static const struct kernfs_ops sysfs_bin_kfops_wo = {
191 .write = sysfs_kf_bin_write,
192};
193
194static const struct kernfs_ops sysfs_bin_kfops_rw = {
195 .read = sysfs_kf_bin_read,
196 .write = sysfs_kf_bin_write,
Tejun Heo9b2db6e2013-12-10 09:29:17 -0500197};
198
199static const struct kernfs_ops sysfs_bin_kfops_mmap = {
200 .read = sysfs_kf_bin_read,
201 .write = sysfs_kf_bin_write,
Tejun Heof6acf8b2013-11-28 14:54:21 -0500202 .mmap = sysfs_kf_bin_mmap,
203};
204
Tejun Heo324a56e2013-12-11 14:11:53 -0500205int sysfs_add_file_mode_ns(struct kernfs_node *parent,
Tejun Heoa7dc66d2013-11-28 14:54:23 -0500206 const struct attribute *attr, bool is_bin,
Tejun Heo496f7392013-11-28 14:54:24 -0500207 umode_t mode, const void *ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
Tejun Heo517e64f2013-11-28 14:54:29 -0500209 struct lock_class_key *key = NULL;
Tejun Heof6acf8b2013-11-28 14:54:21 -0500210 const struct kernfs_ops *ops;
Tejun Heo324a56e2013-12-11 14:11:53 -0500211 struct kernfs_node *kn;
Tejun Heo471bd7b2013-11-28 14:54:22 -0500212 loff_t size;
Tejun Heoa26cd722007-06-14 03:45:14 +0900213
Tejun Heoa7dc66d2013-11-28 14:54:23 -0500214 if (!is_bin) {
Tejun Heo324a56e2013-12-11 14:11:53 -0500215 struct kobject *kobj = parent->priv;
Tejun Heof6acf8b2013-11-28 14:54:21 -0500216 const struct sysfs_ops *sysfs_ops = kobj->ktype->sysfs_ops;
217
218 /* every kobject with an attribute needs a ktype assigned */
219 if (WARN(!sysfs_ops, KERN_ERR
220 "missing sysfs attribute operations for kobject: %s\n",
221 kobject_name(kobj)))
222 return -EINVAL;
223
224 if (sysfs_ops->show && sysfs_ops->store)
225 ops = &sysfs_file_kfops_rw;
226 else if (sysfs_ops->show)
227 ops = &sysfs_file_kfops_ro;
228 else if (sysfs_ops->store)
229 ops = &sysfs_file_kfops_wo;
230 else
231 ops = &sysfs_file_kfops_empty;
Tejun Heo471bd7b2013-11-28 14:54:22 -0500232
233 size = PAGE_SIZE;
Tejun Heof6acf8b2013-11-28 14:54:21 -0500234 } else {
235 struct bin_attribute *battr = (void *)attr;
236
Tejun Heo9b2db6e2013-12-10 09:29:17 -0500237 if (battr->mmap)
238 ops = &sysfs_bin_kfops_mmap;
239 else if (battr->read && battr->write)
Tejun Heof6acf8b2013-11-28 14:54:21 -0500240 ops = &sysfs_bin_kfops_rw;
241 else if (battr->read)
242 ops = &sysfs_bin_kfops_ro;
243 else if (battr->write)
244 ops = &sysfs_bin_kfops_wo;
245 else
246 ops = &sysfs_file_kfops_empty;
Tejun Heo471bd7b2013-11-28 14:54:22 -0500247
248 size = battr->size;
Tejun Heof6acf8b2013-11-28 14:54:21 -0500249 }
250
Tejun Heo517e64f2013-11-28 14:54:29 -0500251#ifdef CONFIG_DEBUG_LOCK_ALLOC
252 if (!attr->ignore_lockdep)
253 key = attr->key ?: (struct lock_class_key *)&attr->skey;
254#endif
Tejun Heo2063d602013-12-11 16:02:57 -0500255 kn = __kernfs_create_file(parent, attr->name, mode, size, ops,
256 (void *)attr, ns, true, key);
Tejun Heo324a56e2013-12-11 14:11:53 -0500257 if (IS_ERR(kn)) {
258 if (PTR_ERR(kn) == -EEXIST)
259 sysfs_warn_dup(parent, attr->name);
260 return PTR_ERR(kn);
Tejun Heo496f7392013-11-28 14:54:24 -0500261 }
262 return 0;
263}
264
Tejun Heo324a56e2013-12-11 14:11:53 -0500265int sysfs_add_file(struct kernfs_node *parent, const struct attribute *attr,
Tejun Heoa7dc66d2013-11-28 14:54:23 -0500266 bool is_bin)
James Bottomley0f423892008-03-20 20:47:52 -0500267{
Tejun Heo324a56e2013-12-11 14:11:53 -0500268 return sysfs_add_file_mode_ns(parent, attr, is_bin, attr->mode, NULL);
James Bottomley0f423892008-03-20 20:47:52 -0500269}
270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271/**
Tejun Heo58292cbe2013-09-11 22:29:04 -0400272 * sysfs_create_file_ns - create an attribute file for an object with custom ns
273 * @kobj: object we're creating for
274 * @attr: attribute descriptor
275 * @ns: namespace the new file should belong to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 */
Tejun Heo58292cbe2013-09-11 22:29:04 -0400277int sysfs_create_file_ns(struct kobject *kobj, const struct attribute *attr,
278 const void *ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
Tejun Heo608e2662007-06-14 04:27:22 +0900280 BUG_ON(!kobj || !kobj->sd || !attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Tejun Heoa7dc66d2013-11-28 14:54:23 -0500282 return sysfs_add_file_mode_ns(kobj->sd, attr, false, attr->mode, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284}
Tejun Heo58292cbe2013-09-11 22:29:04 -0400285EXPORT_SYMBOL_GPL(sysfs_create_file_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Andi Kleen1c205ae2010-01-05 12:48:01 +0100287int sysfs_create_files(struct kobject *kobj, const struct attribute **ptr)
288{
289 int err = 0;
290 int i;
291
292 for (i = 0; ptr[i] && !err; i++)
293 err = sysfs_create_file(kobj, ptr[i]);
294 if (err)
295 while (--i >= 0)
296 sysfs_remove_file(kobj, ptr[i]);
297 return err;
298}
Greg Kroah-Hartman1b866752013-08-21 16:17:47 -0700299EXPORT_SYMBOL_GPL(sysfs_create_files);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301/**
Alan Sterndfa87c82007-02-20 15:02:44 -0500302 * sysfs_add_file_to_group - add an attribute file to a pre-existing group.
303 * @kobj: object we're acting for.
304 * @attr: attribute descriptor.
305 * @group: group name.
306 */
307int sysfs_add_file_to_group(struct kobject *kobj,
308 const struct attribute *attr, const char *group)
309{
Tejun Heo324a56e2013-12-11 14:11:53 -0500310 struct kernfs_node *parent;
Alan Sterndfa87c82007-02-20 15:02:44 -0500311 int error;
312
Tejun Heoccf73cf2013-11-28 14:54:30 -0500313 if (group) {
Tejun Heo324a56e2013-12-11 14:11:53 -0500314 parent = kernfs_find_and_get(kobj->sd, group);
Tejun Heoccf73cf2013-11-28 14:54:30 -0500315 } else {
Tejun Heo324a56e2013-12-11 14:11:53 -0500316 parent = kobj->sd;
317 kernfs_get(parent);
Tejun Heoccf73cf2013-11-28 14:54:30 -0500318 }
James Bottomley11f24fb2008-01-02 18:44:05 -0600319
Tejun Heo324a56e2013-12-11 14:11:53 -0500320 if (!parent)
Tejun Heo608e2662007-06-14 04:27:22 +0900321 return -ENOENT;
322
Tejun Heo324a56e2013-12-11 14:11:53 -0500323 error = sysfs_add_file(parent, attr, false);
324 kernfs_put(parent);
Tejun Heo608e2662007-06-14 04:27:22 +0900325
Alan Sterndfa87c82007-02-20 15:02:44 -0500326 return error;
327}
328EXPORT_SYMBOL_GPL(sysfs_add_file_to_group);
329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330/**
Kay Sievers31e5abe2005-04-18 21:57:32 -0700331 * sysfs_chmod_file - update the modified mode value on an object attribute.
332 * @kobj: object we're acting for.
333 * @attr: attribute descriptor.
334 * @mode: file permissions.
335 *
336 */
Jean Delvare49c19402010-07-02 16:54:05 +0200337int sysfs_chmod_file(struct kobject *kobj, const struct attribute *attr,
Al Viro48176a92011-07-24 03:40:40 -0400338 umode_t mode)
Kay Sievers31e5abe2005-04-18 21:57:32 -0700339{
Tejun Heo324a56e2013-12-11 14:11:53 -0500340 struct kernfs_node *kn;
Maneesh Sonibc062b12005-07-29 12:13:35 -0700341 struct iattr newattrs;
Tejun Heo51225032007-06-14 04:27:25 +0900342 int rc;
Kay Sievers31e5abe2005-04-18 21:57:32 -0700343
Tejun Heo324a56e2013-12-11 14:11:53 -0500344 kn = kernfs_find_and_get(kobj->sd, attr->name);
345 if (!kn)
Tejun Heo5d604182013-11-23 17:21:52 -0500346 return -ENOENT;
Tejun Heo51225032007-06-14 04:27:25 +0900347
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500348 newattrs.ia_mode = (mode & S_IALLUGO) | (kn->mode & ~S_IALLUGO);
Eric W. Biederman4c6974f2009-11-07 23:27:02 -0800349 newattrs.ia_valid = ATTR_MODE;
Tejun Heof88123e2007-09-20 16:05:10 +0900350
Tejun Heo324a56e2013-12-11 14:11:53 -0500351 rc = kernfs_setattr(kn, &newattrs);
Tejun Heo5d604182013-11-23 17:21:52 -0500352
Tejun Heo324a56e2013-12-11 14:11:53 -0500353 kernfs_put(kn);
Tejun Heo51225032007-06-14 04:27:25 +0900354 return rc;
Kay Sievers31e5abe2005-04-18 21:57:32 -0700355}
356EXPORT_SYMBOL_GPL(sysfs_chmod_file);
357
Kay Sievers31e5abe2005-04-18 21:57:32 -0700358/**
Tejun Heo58292cbe2013-09-11 22:29:04 -0400359 * sysfs_remove_file_ns - remove an object attribute with a custom ns tag
360 * @kobj: object we're acting for
361 * @attr: attribute descriptor
362 * @ns: namespace tag of the file to remove
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 *
Tejun Heo58292cbe2013-09-11 22:29:04 -0400364 * Hash the attribute name and namespace tag and kill the victim.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 */
Tejun Heo58292cbe2013-09-11 22:29:04 -0400366void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr,
367 const void *ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
Tejun Heo324a56e2013-12-11 14:11:53 -0500369 struct kernfs_node *parent = kobj->sd;
Eric W. Biederman487505c2011-10-12 21:53:38 +0000370
Tejun Heo324a56e2013-12-11 14:11:53 -0500371 kernfs_remove_by_name_ns(parent, attr->name, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372}
Tejun Heo58292cbe2013-09-11 22:29:04 -0400373EXPORT_SYMBOL_GPL(sysfs_remove_file_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Tejun Heo6b0afc22014-02-03 14:03:01 -0500375/**
376 * sysfs_remove_file_self - remove an object attribute from its own method
377 * @kobj: object we're acting for
378 * @attr: attribute descriptor
379 *
380 * See kernfs_remove_self() for details.
381 */
382bool sysfs_remove_file_self(struct kobject *kobj, const struct attribute *attr)
383{
384 struct kernfs_node *parent = kobj->sd;
385 struct kernfs_node *kn;
386 bool ret;
387
388 kn = kernfs_find_and_get(parent, attr->name);
389 if (WARN_ON_ONCE(!kn))
390 return false;
391
392 ret = kernfs_remove_self(kn);
393
394 kernfs_put(kn);
395 return ret;
396}
397
Greg Kroah-Hartman1b18dc22013-08-21 16:28:26 -0700398void sysfs_remove_files(struct kobject *kobj, const struct attribute **ptr)
Andi Kleen1c205ae2010-01-05 12:48:01 +0100399{
400 int i;
401 for (i = 0; ptr[i]; i++)
402 sysfs_remove_file(kobj, ptr[i]);
403}
Greg Kroah-Hartman1b866752013-08-21 16:17:47 -0700404EXPORT_SYMBOL_GPL(sysfs_remove_files);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Alan Sterndfa87c82007-02-20 15:02:44 -0500406/**
407 * sysfs_remove_file_from_group - remove an attribute file from a group.
408 * @kobj: object we're acting for.
409 * @attr: attribute descriptor.
410 * @group: group name.
411 */
412void sysfs_remove_file_from_group(struct kobject *kobj,
413 const struct attribute *attr, const char *group)
414{
Tejun Heo324a56e2013-12-11 14:11:53 -0500415 struct kernfs_node *parent;
Alan Sterndfa87c82007-02-20 15:02:44 -0500416
Tejun Heoccf73cf2013-11-28 14:54:30 -0500417 if (group) {
Tejun Heo324a56e2013-12-11 14:11:53 -0500418 parent = kernfs_find_and_get(kobj->sd, group);
Tejun Heoccf73cf2013-11-28 14:54:30 -0500419 } else {
Tejun Heo324a56e2013-12-11 14:11:53 -0500420 parent = kobj->sd;
421 kernfs_get(parent);
Tejun Heoccf73cf2013-11-28 14:54:30 -0500422 }
423
Tejun Heo324a56e2013-12-11 14:11:53 -0500424 if (parent) {
425 kernfs_remove_by_name(parent, attr->name);
426 kernfs_put(parent);
Alan Sterndfa87c82007-02-20 15:02:44 -0500427 }
428}
429EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group);
430
Tejun Heo3124eb12013-10-01 17:42:09 -0400431/**
432 * sysfs_create_bin_file - create binary file for object.
433 * @kobj: object.
434 * @attr: attribute descriptor.
435 */
436int sysfs_create_bin_file(struct kobject *kobj,
437 const struct bin_attribute *attr)
438{
439 BUG_ON(!kobj || !kobj->sd || !attr);
440
Tejun Heoa7dc66d2013-11-28 14:54:23 -0500441 return sysfs_add_file(kobj->sd, &attr->attr, true);
Tejun Heo3124eb12013-10-01 17:42:09 -0400442}
443EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
444
445/**
446 * sysfs_remove_bin_file - remove binary file for object.
447 * @kobj: object.
448 * @attr: attribute descriptor.
449 */
450void sysfs_remove_bin_file(struct kobject *kobj,
451 const struct bin_attribute *attr)
452{
Tejun Heo879f40d2013-11-23 17:21:49 -0500453 kernfs_remove_by_name(kobj->sd, attr->attr.name);
Tejun Heo3124eb12013-10-01 17:42:09 -0400454}
455EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);
Greg Kroah-Hartman72099302014-03-25 20:54:57 -0700456
457struct sysfs_schedule_callback_struct {
458 struct list_head workq_list;
459 struct kobject *kobj;
460 void (*func)(void *);
461 void *data;
462 struct module *owner;
463 struct work_struct work;
464};
465
466static struct workqueue_struct *sysfs_workqueue;
467static DEFINE_MUTEX(sysfs_workq_mutex);
468static LIST_HEAD(sysfs_workq);
469static void sysfs_schedule_callback_work(struct work_struct *work)
470{
471 struct sysfs_schedule_callback_struct *ss = container_of(work,
472 struct sysfs_schedule_callback_struct, work);
473
474 (ss->func)(ss->data);
475 kobject_put(ss->kobj);
476 module_put(ss->owner);
477 mutex_lock(&sysfs_workq_mutex);
478 list_del(&ss->workq_list);
479 mutex_unlock(&sysfs_workq_mutex);
480 kfree(ss);
481}
482
483/**
484 * sysfs_schedule_callback - helper to schedule a callback for a kobject
485 * @kobj: object we're acting for.
486 * @func: callback function to invoke later.
487 * @data: argument to pass to @func.
488 * @owner: module owning the callback code
489 *
490 * sysfs attribute methods must not unregister themselves or their parent
491 * kobject (which would amount to the same thing). Attempts to do so will
492 * deadlock, since unregistration is mutually exclusive with driver
493 * callbacks.
494 *
495 * Instead methods can call this routine, which will attempt to allocate
496 * and schedule a workqueue request to call back @func with @data as its
497 * argument in the workqueue's process context. @kobj will be pinned
498 * until @func returns.
499 *
500 * Returns 0 if the request was submitted, -ENOMEM if storage could not
501 * be allocated, -ENODEV if a reference to @owner isn't available,
502 * -EAGAIN if a callback has already been scheduled for @kobj.
503 */
504int sysfs_schedule_callback(struct kobject *kobj, void (*func)(void *),
505 void *data, struct module *owner)
506{
507 struct sysfs_schedule_callback_struct *ss, *tmp;
508
509 if (!try_module_get(owner))
510 return -ENODEV;
511
512 mutex_lock(&sysfs_workq_mutex);
513 list_for_each_entry_safe(ss, tmp, &sysfs_workq, workq_list)
514 if (ss->kobj == kobj) {
515 module_put(owner);
516 mutex_unlock(&sysfs_workq_mutex);
517 return -EAGAIN;
518 }
519 mutex_unlock(&sysfs_workq_mutex);
520
521 if (sysfs_workqueue == NULL) {
522 sysfs_workqueue = create_singlethread_workqueue("sysfsd");
523 if (sysfs_workqueue == NULL) {
524 module_put(owner);
525 return -ENOMEM;
526 }
527 }
528
529 ss = kmalloc(sizeof(*ss), GFP_KERNEL);
530 if (!ss) {
531 module_put(owner);
532 return -ENOMEM;
533 }
534 kobject_get(kobj);
535 ss->kobj = kobj;
536 ss->func = func;
537 ss->data = data;
538 ss->owner = owner;
539 INIT_WORK(&ss->work, sysfs_schedule_callback_work);
540 INIT_LIST_HEAD(&ss->workq_list);
541 mutex_lock(&sysfs_workq_mutex);
542 list_add_tail(&ss->workq_list, &sysfs_workq);
543 mutex_unlock(&sysfs_workq_mutex);
544 queue_work(sysfs_workqueue, &ss->work);
545 return 0;
546}
547EXPORT_SYMBOL_GPL(sysfs_schedule_callback);